]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/contrib/dev/acpica/events/evregion.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / contrib / dev / acpica / 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 - 2009, 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 <contrib/dev/acpica/include/acpi.h>
120 #include <contrib/dev/acpica/include/accommon.h>
121 #include <contrib/dev/acpica/include/acevents.h>
122 #include <contrib/dev/acpica/include/acnamesp.h>
123 #include <contrib/dev/acpica/include/acinterp.h>
124
125 #define _COMPONENT          ACPI_EVENTS
126         ACPI_MODULE_NAME    ("evregion")
127
128
129 /* Local prototypes */
130
131 static ACPI_STATUS
132 AcpiEvRegRun (
133     ACPI_HANDLE             ObjHandle,
134     UINT32                  Level,
135     void                    *Context,
136     void                    **ReturnValue);
137
138 static ACPI_STATUS
139 AcpiEvInstallHandler (
140     ACPI_HANDLE             ObjHandle,
141     UINT32                  Level,
142     void                    *Context,
143     void                    **ReturnValue);
144
145 /* These are the address spaces that will get default handlers */
146
147 #define ACPI_NUM_DEFAULT_SPACES     4
148
149 static UINT8        AcpiGbl_DefaultAddressSpaces[ACPI_NUM_DEFAULT_SPACES] =
150 {
151     ACPI_ADR_SPACE_SYSTEM_MEMORY,
152     ACPI_ADR_SPACE_SYSTEM_IO,
153     ACPI_ADR_SPACE_PCI_CONFIG,
154     ACPI_ADR_SPACE_DATA_TABLE
155 };
156
157
158 /*******************************************************************************
159  *
160  * FUNCTION:    AcpiEvInstallRegionHandlers
161  *
162  * PARAMETERS:  None
163  *
164  * RETURN:      Status
165  *
166  * DESCRIPTION: Installs the core subsystem default address space handlers.
167  *
168  ******************************************************************************/
169
170 ACPI_STATUS
171 AcpiEvInstallRegionHandlers (
172     void)
173 {
174     ACPI_STATUS             Status;
175     UINT32                  i;
176
177
178     ACPI_FUNCTION_TRACE (EvInstallRegionHandlers);
179
180
181     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
182     if (ACPI_FAILURE (Status))
183     {
184         return_ACPI_STATUS (Status);
185     }
186
187     /*
188      * All address spaces (PCI Config, EC, SMBus) are scope dependent and
189      * registration must occur for a specific device.
190      *
191      * In the case of the system memory and IO address spaces there is
192      * currently no device associated with the address space. For these we
193      * use the root.
194      *
195      * We install the default PCI config space handler at the root so that
196      * this space is immediately available even though the we have not
197      * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
198      * specification which states that the PCI config space must be always
199      * available -- even though we are nowhere near ready to find the PCI root
200      * buses at this point.
201      *
202      * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
203      * has already been installed (via AcpiInstallAddressSpaceHandler).
204      * Similar for AE_SAME_HANDLER.
205      */
206     for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
207     {
208         Status = AcpiEvInstallSpaceHandler (AcpiGbl_RootNode,
209                     AcpiGbl_DefaultAddressSpaces[i],
210                     ACPI_DEFAULT_HANDLER, NULL, NULL);
211         switch (Status)
212         {
213         case AE_OK:
214         case AE_SAME_HANDLER:
215         case AE_ALREADY_EXISTS:
216
217             /* These exceptions are all OK */
218
219             Status = AE_OK;
220             break;
221
222         default:
223
224             goto UnlockAndExit;
225         }
226     }
227
228 UnlockAndExit:
229     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
230     return_ACPI_STATUS (Status);
231 }
232
233
234 /*******************************************************************************
235  *
236  * FUNCTION:    AcpiEvInitializeOpRegions
237  *
238  * PARAMETERS:  None
239  *
240  * RETURN:      Status
241  *
242  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
243  *              an installed default region handler.
244  *
245  ******************************************************************************/
246
247 ACPI_STATUS
248 AcpiEvInitializeOpRegions (
249     void)
250 {
251     ACPI_STATUS             Status;
252     UINT32                  i;
253
254
255     ACPI_FUNCTION_TRACE (EvInitializeOpRegions);
256
257
258     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
259     if (ACPI_FAILURE (Status))
260     {
261         return_ACPI_STATUS (Status);
262     }
263
264     /* Run the _REG methods for OpRegions in each default address space */
265
266     for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
267     {
268         /*
269          * TBD: Make sure handler is the DEFAULT handler, otherwise
270          * _REG will have already been run.
271          */
272         Status = AcpiEvExecuteRegMethods (AcpiGbl_RootNode,
273                     AcpiGbl_DefaultAddressSpaces[i]);
274     }
275
276     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
277     return_ACPI_STATUS (Status);
278 }
279
280
281 /*******************************************************************************
282  *
283  * FUNCTION:    AcpiEvExecuteRegMethod
284  *
285  * PARAMETERS:  RegionObj           - Region object
286  *              Function            - Passed to _REG: On (1) or Off (0)
287  *
288  * RETURN:      Status
289  *
290  * DESCRIPTION: Execute _REG method for a region
291  *
292  ******************************************************************************/
293
294 ACPI_STATUS
295 AcpiEvExecuteRegMethod (
296     ACPI_OPERAND_OBJECT     *RegionObj,
297     UINT32                  Function)
298 {
299     ACPI_EVALUATE_INFO      *Info;
300     ACPI_OPERAND_OBJECT     *Args[3];
301     ACPI_OPERAND_OBJECT     *RegionObj2;
302     ACPI_STATUS             Status;
303
304
305     ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
306
307
308     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
309     if (!RegionObj2)
310     {
311         return_ACPI_STATUS (AE_NOT_EXIST);
312     }
313
314     if (RegionObj2->Extra.Method_REG == NULL)
315     {
316         return_ACPI_STATUS (AE_OK);
317     }
318
319     /* Allocate and initialize the evaluation information block */
320
321     Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
322     if (!Info)
323     {
324         return_ACPI_STATUS (AE_NO_MEMORY);
325     }
326
327     Info->PrefixNode = RegionObj2->Extra.Method_REG;
328     Info->Pathname = NULL;
329     Info->Parameters = Args;
330     Info->Flags = ACPI_IGNORE_RETURN_VALUE;
331
332     /*
333      * The _REG method has two arguments:
334      *
335      * Arg0 - Integer:
336      *  Operation region space ID Same value as RegionObj->Region.SpaceId
337      *
338      * Arg1 - Integer:
339      *  connection status 1 for connecting the handler, 0 for disconnecting
340      *  the handler (Passed as a parameter)
341      */
342     Args[0] = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
343     if (!Args[0])
344     {
345         Status = AE_NO_MEMORY;
346         goto Cleanup1;
347     }
348
349     Args[1] = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
350     if (!Args[1])
351     {
352         Status = AE_NO_MEMORY;
353         goto Cleanup2;
354     }
355
356     /* Setup the parameter objects */
357
358     Args[0]->Integer.Value = RegionObj->Region.SpaceId;
359     Args[1]->Integer.Value = Function;
360     Args[2] = NULL;
361
362     /* Execute the method, no return value */
363
364     ACPI_DEBUG_EXEC (
365         AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
366
367     Status = AcpiNsEvaluate (Info);
368     AcpiUtRemoveReference (Args[1]);
369
370 Cleanup2:
371     AcpiUtRemoveReference (Args[0]);
372
373 Cleanup1:
374     ACPI_FREE (Info);
375     return_ACPI_STATUS (Status);
376 }
377
378
379 /*******************************************************************************
380  *
381  * FUNCTION:    AcpiEvAddressSpaceDispatch
382  *
383  * PARAMETERS:  RegionObj           - Internal region object
384  *              Function            - Read or Write operation
385  *              RegionOffset        - Where in the region to read or write
386  *              BitWidth            - Field width in bits (8, 16, 32, or 64)
387  *              Value               - Pointer to in or out value, must be
388  *                                    full 64-bit ACPI_INTEGER
389  *
390  * RETURN:      Status
391  *
392  * DESCRIPTION: Dispatch an address space or operation region access to
393  *              a previously installed handler.
394  *
395  ******************************************************************************/
396
397 ACPI_STATUS
398 AcpiEvAddressSpaceDispatch (
399     ACPI_OPERAND_OBJECT     *RegionObj,
400     UINT32                  Function,
401     UINT32                  RegionOffset,
402     UINT32                  BitWidth,
403     ACPI_INTEGER            *Value)
404 {
405     ACPI_STATUS             Status;
406     ACPI_ADR_SPACE_HANDLER  Handler;
407     ACPI_ADR_SPACE_SETUP    RegionSetup;
408     ACPI_OPERAND_OBJECT     *HandlerDesc;
409     ACPI_OPERAND_OBJECT     *RegionObj2;
410     void                    *RegionContext = NULL;
411
412
413     ACPI_FUNCTION_TRACE (EvAddressSpaceDispatch);
414
415
416     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
417     if (!RegionObj2)
418     {
419         return_ACPI_STATUS (AE_NOT_EXIST);
420     }
421
422     /* Ensure that there is a handler associated with this region */
423
424     HandlerDesc = RegionObj->Region.Handler;
425     if (!HandlerDesc)
426     {
427         ACPI_ERROR ((AE_INFO,
428             "No handler for Region [%4.4s] (%p) [%s]",
429             AcpiUtGetNodeName (RegionObj->Region.Node),
430             RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
431
432         return_ACPI_STATUS (AE_NOT_EXIST);
433     }
434
435     /*
436      * It may be the case that the region has never been initialized.
437      * Some types of regions require special init code
438      */
439     if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
440     {
441         /* This region has not been initialized yet, do it */
442
443         RegionSetup = HandlerDesc->AddressSpace.Setup;
444         if (!RegionSetup)
445         {
446             /* No initialization routine, exit with error */
447
448             ACPI_ERROR ((AE_INFO,
449                 "No init routine for region(%p) [%s]",
450                 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
451             return_ACPI_STATUS (AE_NOT_EXIST);
452         }
453
454         /*
455          * We must exit the interpreter because the region setup will
456          * potentially execute control methods (for example, the _REG method
457          * for this region)
458          */
459         AcpiExExitInterpreter ();
460
461         Status = RegionSetup (RegionObj, ACPI_REGION_ACTIVATE,
462                     HandlerDesc->AddressSpace.Context, &RegionContext);
463
464         /* Re-enter the interpreter */
465
466         AcpiExEnterInterpreter ();
467
468         /* Check for failure of the Region Setup */
469
470         if (ACPI_FAILURE (Status))
471         {
472             ACPI_EXCEPTION ((AE_INFO, Status,
473                 "During region initialization: [%s]",
474                 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
475             return_ACPI_STATUS (Status);
476         }
477
478         /* Region initialization may have been completed by RegionSetup */
479
480         if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
481         {
482             RegionObj->Region.Flags |= AOPOBJ_SETUP_COMPLETE;
483
484             if (RegionObj2->Extra.RegionContext)
485             {
486                 /* The handler for this region was already installed */
487
488                 ACPI_FREE (RegionContext);
489             }
490             else
491             {
492                 /*
493                  * Save the returned context for use in all accesses to
494                  * this particular region
495                  */
496                 RegionObj2->Extra.RegionContext = RegionContext;
497             }
498         }
499     }
500
501     /* We have everything we need, we can invoke the address space handler */
502
503     Handler = HandlerDesc->AddressSpace.Handler;
504
505     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
506         "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
507         &RegionObj->Region.Handler->AddressSpace, Handler,
508         ACPI_FORMAT_NATIVE_UINT (RegionObj->Region.Address + RegionOffset),
509         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
510
511     if (!(HandlerDesc->AddressSpace.HandlerFlags &
512             ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
513     {
514         /*
515          * For handlers other than the default (supplied) handlers, we must
516          * exit the interpreter because the handler *might* block -- we don't
517          * know what it will do, so we can't hold the lock on the intepreter.
518          */
519         AcpiExExitInterpreter();
520     }
521
522     /* Call the handler */
523
524     Status = Handler (Function,
525         (RegionObj->Region.Address + RegionOffset), BitWidth, Value,
526         HandlerDesc->AddressSpace.Context, RegionObj2->Extra.RegionContext);
527
528     if (ACPI_FAILURE (Status))
529     {
530         ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
531             AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
532     }
533
534     if (!(HandlerDesc->AddressSpace.HandlerFlags &
535             ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
536     {
537         /*
538          * We just returned from a non-default handler, we must re-enter the
539          * interpreter
540          */
541        AcpiExEnterInterpreter ();
542     }
543
544     return_ACPI_STATUS (Status);
545 }
546
547
548 /*******************************************************************************
549  *
550  * FUNCTION:    AcpiEvDetachRegion
551  *
552  * PARAMETERS:  RegionObj           - Region Object
553  *              AcpiNsIsLocked      - Namespace Region Already Locked?
554  *
555  * RETURN:      None
556  *
557  * DESCRIPTION: Break the association between the handler and the region
558  *              this is a two way association.
559  *
560  ******************************************************************************/
561
562 void
563 AcpiEvDetachRegion(
564     ACPI_OPERAND_OBJECT     *RegionObj,
565     BOOLEAN                 AcpiNsIsLocked)
566 {
567     ACPI_OPERAND_OBJECT     *HandlerObj;
568     ACPI_OPERAND_OBJECT     *ObjDesc;
569     ACPI_OPERAND_OBJECT     **LastObjPtr;
570     ACPI_ADR_SPACE_SETUP    RegionSetup;
571     void                    **RegionContext;
572     ACPI_OPERAND_OBJECT     *RegionObj2;
573     ACPI_STATUS             Status;
574
575
576     ACPI_FUNCTION_TRACE (EvDetachRegion);
577
578
579     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
580     if (!RegionObj2)
581     {
582         return_VOID;
583     }
584     RegionContext = &RegionObj2->Extra.RegionContext;
585
586     /* Get the address handler from the region object */
587
588     HandlerObj = RegionObj->Region.Handler;
589     if (!HandlerObj)
590     {
591         /* This region has no handler, all done */
592
593         return_VOID;
594     }
595
596     /* Find this region in the handler's list */
597
598     ObjDesc = HandlerObj->AddressSpace.RegionList;
599     LastObjPtr = &HandlerObj->AddressSpace.RegionList;
600
601     while (ObjDesc)
602     {
603         /* Is this the correct Region? */
604
605         if (ObjDesc == RegionObj)
606         {
607             ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
608                 "Removing Region %p from address handler %p\n",
609                 RegionObj, HandlerObj));
610
611             /* This is it, remove it from the handler's list */
612
613             *LastObjPtr = ObjDesc->Region.Next;
614             ObjDesc->Region.Next = NULL;        /* Must clear field */
615
616             if (AcpiNsIsLocked)
617             {
618                 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
619                 if (ACPI_FAILURE (Status))
620                 {
621                     return_VOID;
622                 }
623             }
624
625             /* Now stop region accesses by executing the _REG method */
626
627             Status = AcpiEvExecuteRegMethod (RegionObj, 0);
628             if (ACPI_FAILURE (Status))
629             {
630                 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
631                     AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
632             }
633
634             if (AcpiNsIsLocked)
635             {
636                 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
637                 if (ACPI_FAILURE (Status))
638                 {
639                     return_VOID;
640                 }
641             }
642
643             /*
644              * If the region has been activated, call the setup handler with
645              * the deactivate notification
646              */
647             if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
648             {
649                 RegionSetup = HandlerObj->AddressSpace.Setup;
650                 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
651                     HandlerObj->AddressSpace.Context, RegionContext);
652
653                 /* Init routine may fail, Just ignore errors */
654
655                 if (ACPI_FAILURE (Status))
656                 {
657                     ACPI_EXCEPTION ((AE_INFO, Status,
658                         "from region handler - deactivate, [%s]",
659                         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
660                 }
661
662                 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
663             }
664
665             /*
666              * Remove handler reference in the region
667              *
668              * NOTE: this doesn't mean that the region goes away, the region
669              * is just inaccessible as indicated to the _REG method
670              *
671              * If the region is on the handler's list, this must be the
672              * region's handler
673              */
674             RegionObj->Region.Handler = NULL;
675             AcpiUtRemoveReference (HandlerObj);
676
677             return_VOID;
678         }
679
680         /* Walk the linked list of handlers */
681
682         LastObjPtr = &ObjDesc->Region.Next;
683         ObjDesc = ObjDesc->Region.Next;
684     }
685
686     /* If we get here, the region was not in the handler's region list */
687
688     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
689         "Cannot remove region %p from address handler %p\n",
690         RegionObj, HandlerObj));
691
692     return_VOID;
693 }
694
695
696 /*******************************************************************************
697  *
698  * FUNCTION:    AcpiEvAttachRegion
699  *
700  * PARAMETERS:  HandlerObj          - Handler Object
701  *              RegionObj           - Region Object
702  *              AcpiNsIsLocked      - Namespace Region Already Locked?
703  *
704  * RETURN:      None
705  *
706  * DESCRIPTION: Create the association between the handler and the region
707  *              this is a two way association.
708  *
709  ******************************************************************************/
710
711 ACPI_STATUS
712 AcpiEvAttachRegion (
713     ACPI_OPERAND_OBJECT     *HandlerObj,
714     ACPI_OPERAND_OBJECT     *RegionObj,
715     BOOLEAN                 AcpiNsIsLocked)
716 {
717
718     ACPI_FUNCTION_TRACE (EvAttachRegion);
719
720
721     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
722         "Adding Region [%4.4s] %p to address handler %p [%s]\n",
723         AcpiUtGetNodeName (RegionObj->Region.Node),
724         RegionObj, HandlerObj,
725         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
726
727     /* Link this region to the front of the handler's list */
728
729     RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
730     HandlerObj->AddressSpace.RegionList = RegionObj;
731
732     /* Install the region's handler */
733
734     if (RegionObj->Region.Handler)
735     {
736         return_ACPI_STATUS (AE_ALREADY_EXISTS);
737     }
738
739     RegionObj->Region.Handler = HandlerObj;
740     AcpiUtAddReference (HandlerObj);
741
742     return_ACPI_STATUS (AE_OK);
743 }
744
745
746 /*******************************************************************************
747  *
748  * FUNCTION:    AcpiEvInstallHandler
749  *
750  * PARAMETERS:  WalkNamespace callback
751  *
752  * DESCRIPTION: This routine installs an address handler into objects that are
753  *              of type Region or Device.
754  *
755  *              If the Object is a Device, and the device has a handler of
756  *              the same type then the search is terminated in that branch.
757  *
758  *              This is because the existing handler is closer in proximity
759  *              to any more regions than the one we are trying to install.
760  *
761  ******************************************************************************/
762
763 static ACPI_STATUS
764 AcpiEvInstallHandler (
765     ACPI_HANDLE             ObjHandle,
766     UINT32                  Level,
767     void                    *Context,
768     void                    **ReturnValue)
769 {
770     ACPI_OPERAND_OBJECT     *HandlerObj;
771     ACPI_OPERAND_OBJECT     *NextHandlerObj;
772     ACPI_OPERAND_OBJECT     *ObjDesc;
773     ACPI_NAMESPACE_NODE     *Node;
774     ACPI_STATUS             Status;
775
776
777     ACPI_FUNCTION_NAME (EvInstallHandler);
778
779
780     HandlerObj = (ACPI_OPERAND_OBJECT  *) Context;
781
782     /* Parameter validation */
783
784     if (!HandlerObj)
785     {
786         return (AE_OK);
787     }
788
789     /* Convert and validate the device handle */
790
791     Node = AcpiNsMapHandleToNode (ObjHandle);
792     if (!Node)
793     {
794         return (AE_BAD_PARAMETER);
795     }
796
797     /*
798      * We only care about regions and objects that are allowed to have
799      * address space handlers
800      */
801     if ((Node->Type != ACPI_TYPE_DEVICE) &&
802         (Node->Type != ACPI_TYPE_REGION) &&
803         (Node != AcpiGbl_RootNode))
804     {
805         return (AE_OK);
806     }
807
808     /* Check for an existing internal object */
809
810     ObjDesc = AcpiNsGetAttachedObject (Node);
811     if (!ObjDesc)
812     {
813         /* No object, just exit */
814
815         return (AE_OK);
816     }
817
818     /* Devices are handled different than regions */
819
820     if (ObjDesc->Common.Type == ACPI_TYPE_DEVICE)
821     {
822         /* Check if this Device already has a handler for this address space */
823
824         NextHandlerObj = ObjDesc->Device.Handler;
825         while (NextHandlerObj)
826         {
827             /* Found a handler, is it for the same address space? */
828
829             if (NextHandlerObj->AddressSpace.SpaceId ==
830                     HandlerObj->AddressSpace.SpaceId)
831             {
832                 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
833                     "Found handler for region [%s] in device %p(%p) "
834                     "handler %p\n",
835                     AcpiUtGetRegionName (HandlerObj->AddressSpace.SpaceId),
836                     ObjDesc, NextHandlerObj, HandlerObj));
837
838                 /*
839                  * Since the object we found it on was a device, then it
840                  * means that someone has already installed a handler for
841                  * the branch of the namespace from this device on. Just
842                  * bail out telling the walk routine to not traverse this
843                  * branch. This preserves the scoping rule for handlers.
844                  */
845                 return (AE_CTRL_DEPTH);
846             }
847
848             /* Walk the linked list of handlers attached to this device */
849
850             NextHandlerObj = NextHandlerObj->AddressSpace.Next;
851         }
852
853         /*
854          * As long as the device didn't have a handler for this space we
855          * don't care about it. We just ignore it and proceed.
856          */
857         return (AE_OK);
858     }
859
860     /* Object is a Region */
861
862     if (ObjDesc->Region.SpaceId != HandlerObj->AddressSpace.SpaceId)
863     {
864         /* This region is for a different address space, just ignore it */
865
866         return (AE_OK);
867     }
868
869     /*
870      * Now we have a region and it is for the handler's address space type.
871      *
872      * First disconnect region for any previous handler (if any)
873      */
874     AcpiEvDetachRegion (ObjDesc, FALSE);
875
876     /* Connect the region to the new handler */
877
878     Status = AcpiEvAttachRegion (HandlerObj, ObjDesc, FALSE);
879     return (Status);
880 }
881
882
883 /*******************************************************************************
884  *
885  * FUNCTION:    AcpiEvInstallSpaceHandler
886  *
887  * PARAMETERS:  Node            - Namespace node for the device
888  *              SpaceId         - The address space ID
889  *              Handler         - Address of the handler
890  *              Setup           - Address of the setup function
891  *              Context         - Value passed to the handler on each access
892  *
893  * RETURN:      Status
894  *
895  * DESCRIPTION: Install a handler for all OpRegions of a given SpaceId.
896  *              Assumes namespace is locked
897  *
898  ******************************************************************************/
899
900 ACPI_STATUS
901 AcpiEvInstallSpaceHandler (
902     ACPI_NAMESPACE_NODE     *Node,
903     ACPI_ADR_SPACE_TYPE     SpaceId,
904     ACPI_ADR_SPACE_HANDLER  Handler,
905     ACPI_ADR_SPACE_SETUP    Setup,
906     void                    *Context)
907 {
908     ACPI_OPERAND_OBJECT     *ObjDesc;
909     ACPI_OPERAND_OBJECT     *HandlerObj;
910     ACPI_STATUS             Status;
911     ACPI_OBJECT_TYPE        Type;
912     UINT8                  Flags = 0;
913
914
915     ACPI_FUNCTION_TRACE (EvInstallSpaceHandler);
916
917
918     /*
919      * This registration is valid for only the types below and the root. This
920      * is where the default handlers get placed.
921      */
922     if ((Node->Type != ACPI_TYPE_DEVICE)     &&
923         (Node->Type != ACPI_TYPE_PROCESSOR)  &&
924         (Node->Type != ACPI_TYPE_THERMAL)    &&
925         (Node != AcpiGbl_RootNode))
926     {
927         Status = AE_BAD_PARAMETER;
928         goto UnlockAndExit;
929     }
930
931     if (Handler == ACPI_DEFAULT_HANDLER)
932     {
933         Flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
934
935         switch (SpaceId)
936         {
937         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
938             Handler = AcpiExSystemMemorySpaceHandler;
939             Setup   = AcpiEvSystemMemoryRegionSetup;
940             break;
941
942         case ACPI_ADR_SPACE_SYSTEM_IO:
943             Handler = AcpiExSystemIoSpaceHandler;
944             Setup   = AcpiEvIoSpaceRegionSetup;
945             break;
946
947         case ACPI_ADR_SPACE_PCI_CONFIG:
948             Handler = AcpiExPciConfigSpaceHandler;
949             Setup   = AcpiEvPciConfigRegionSetup;
950             break;
951
952         case ACPI_ADR_SPACE_CMOS:
953             Handler = AcpiExCmosSpaceHandler;
954             Setup   = AcpiEvCmosRegionSetup;
955             break;
956
957         case ACPI_ADR_SPACE_PCI_BAR_TARGET:
958             Handler = AcpiExPciBarSpaceHandler;
959             Setup   = AcpiEvPciBarRegionSetup;
960             break;
961
962         case ACPI_ADR_SPACE_DATA_TABLE:
963             Handler = AcpiExDataTableSpaceHandler;
964             Setup   = NULL;
965             break;
966
967         default:
968             Status = AE_BAD_PARAMETER;
969             goto UnlockAndExit;
970         }
971     }
972
973     /* If the caller hasn't specified a setup routine, use the default */
974
975     if (!Setup)
976     {
977         Setup = AcpiEvDefaultRegionSetup;
978     }
979
980     /* Check for an existing internal object */
981
982     ObjDesc = AcpiNsGetAttachedObject (Node);
983     if (ObjDesc)
984     {
985         /*
986          * The attached device object already exists. Make sure the handler
987          * is not already installed.
988          */
989         HandlerObj = ObjDesc->Device.Handler;
990
991         /* Walk the handler list for this device */
992
993         while (HandlerObj)
994         {
995             /* Same SpaceId indicates a handler already installed */
996
997             if (HandlerObj->AddressSpace.SpaceId == SpaceId)
998             {
999                 if (HandlerObj->AddressSpace.Handler == Handler)
1000                 {
1001                     /*
1002                      * It is (relatively) OK to attempt to install the SAME
1003                      * handler twice. This can easily happen with the
1004                      * PCI_Config space.
1005                      */
1006                     Status = AE_SAME_HANDLER;
1007                     goto UnlockAndExit;
1008                 }
1009                 else
1010                 {
1011                     /* A handler is already installed */
1012
1013                     Status = AE_ALREADY_EXISTS;
1014                 }
1015                 goto UnlockAndExit;
1016             }
1017
1018             /* Walk the linked list of handlers */
1019
1020             HandlerObj = HandlerObj->AddressSpace.Next;
1021         }
1022     }
1023     else
1024     {
1025         ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
1026             "Creating object on Device %p while installing handler\n", Node));
1027
1028         /* ObjDesc does not exist, create one */
1029
1030         if (Node->Type == ACPI_TYPE_ANY)
1031         {
1032             Type = ACPI_TYPE_DEVICE;
1033         }
1034         else
1035         {
1036             Type = Node->Type;
1037         }
1038
1039         ObjDesc = AcpiUtCreateInternalObject (Type);
1040         if (!ObjDesc)
1041         {
1042             Status = AE_NO_MEMORY;
1043             goto UnlockAndExit;
1044         }
1045
1046         /* Init new descriptor */
1047
1048         ObjDesc->Common.Type = (UINT8) Type;
1049
1050         /* Attach the new object to the Node */
1051
1052         Status = AcpiNsAttachObject (Node, ObjDesc, Type);
1053
1054         /* Remove local reference to the object */
1055
1056         AcpiUtRemoveReference (ObjDesc);
1057
1058         if (ACPI_FAILURE (Status))
1059         {
1060             goto UnlockAndExit;
1061         }
1062     }
1063
1064     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
1065         "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
1066         AcpiUtGetRegionName (SpaceId), SpaceId,
1067         AcpiUtGetNodeName (Node), Node, ObjDesc));
1068
1069     /*
1070      * Install the handler
1071      *
1072      * At this point there is no existing handler. Just allocate the object
1073      * for the handler and link it into the list.
1074      */
1075     HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
1076     if (!HandlerObj)
1077     {
1078         Status = AE_NO_MEMORY;
1079         goto UnlockAndExit;
1080     }
1081
1082     /* Init handler obj */
1083
1084     HandlerObj->AddressSpace.SpaceId = (UINT8) SpaceId;
1085     HandlerObj->AddressSpace.HandlerFlags = Flags;
1086     HandlerObj->AddressSpace.RegionList = NULL;
1087     HandlerObj->AddressSpace.Node = Node;
1088     HandlerObj->AddressSpace.Handler = Handler;
1089     HandlerObj->AddressSpace.Context = Context;
1090     HandlerObj->AddressSpace.Setup  = Setup;
1091
1092     /* Install at head of Device.AddressSpace list */
1093
1094     HandlerObj->AddressSpace.Next = ObjDesc->Device.Handler;
1095
1096     /*
1097      * The Device object is the first reference on the HandlerObj.
1098      * Each region that uses the handler adds a reference.
1099      */
1100     ObjDesc->Device.Handler = HandlerObj;
1101
1102     /*
1103      * Walk the namespace finding all of the regions this
1104      * handler will manage.
1105      *
1106      * Start at the device and search the branch toward
1107      * the leaf nodes until either the leaf is encountered or
1108      * a device is detected that has an address handler of the
1109      * same type.
1110      *
1111      * In either case, back up and search down the remainder
1112      * of the branch
1113      */
1114     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
1115                 ACPI_NS_WALK_UNLOCK, AcpiEvInstallHandler,
1116                 HandlerObj, NULL);
1117
1118 UnlockAndExit:
1119     return_ACPI_STATUS (Status);
1120 }
1121
1122
1123 /*******************************************************************************
1124  *
1125  * FUNCTION:    AcpiEvExecuteRegMethods
1126  *
1127  * PARAMETERS:  Node            - Namespace node for the device
1128  *              SpaceId         - The address space ID
1129  *
1130  * RETURN:      Status
1131  *
1132  * DESCRIPTION: Run all _REG methods for the input Space ID;
1133  *              Note: assumes namespace is locked, or system init time.
1134  *
1135  ******************************************************************************/
1136
1137 ACPI_STATUS
1138 AcpiEvExecuteRegMethods (
1139     ACPI_NAMESPACE_NODE     *Node,
1140     ACPI_ADR_SPACE_TYPE     SpaceId)
1141 {
1142     ACPI_STATUS             Status;
1143
1144
1145     ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
1146
1147
1148     /*
1149      * Run all _REG methods for all Operation Regions for this space ID. This
1150      * is a separate walk in order to handle any interdependencies between
1151      * regions and _REG methods. (i.e. handlers must be installed for all
1152      * regions of this Space ID before we can run any _REG methods)
1153      */
1154     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
1155                 ACPI_NS_WALK_UNLOCK, AcpiEvRegRun,
1156                 &SpaceId, NULL);
1157
1158     return_ACPI_STATUS (Status);
1159 }
1160
1161
1162 /*******************************************************************************
1163  *
1164  * FUNCTION:    AcpiEvRegRun
1165  *
1166  * PARAMETERS:  WalkNamespace callback
1167  *
1168  * DESCRIPTION: Run _REG method for region objects of the requested spaceID
1169  *
1170  ******************************************************************************/
1171
1172 static ACPI_STATUS
1173 AcpiEvRegRun (
1174     ACPI_HANDLE             ObjHandle,
1175     UINT32                  Level,
1176     void                    *Context,
1177     void                    **ReturnValue)
1178 {
1179     ACPI_OPERAND_OBJECT     *ObjDesc;
1180     ACPI_NAMESPACE_NODE     *Node;
1181     ACPI_ADR_SPACE_TYPE     SpaceId;
1182     ACPI_STATUS             Status;
1183
1184
1185     SpaceId = *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE, Context);
1186
1187     /* Convert and validate the device handle */
1188
1189     Node = AcpiNsMapHandleToNode (ObjHandle);
1190     if (!Node)
1191     {
1192         return (AE_BAD_PARAMETER);
1193     }
1194
1195     /*
1196      * We only care about regions.and objects that are allowed to have address
1197      * space handlers
1198      */
1199     if ((Node->Type != ACPI_TYPE_REGION) &&
1200         (Node != AcpiGbl_RootNode))
1201     {
1202         return (AE_OK);
1203     }
1204
1205     /* Check for an existing internal object */
1206
1207     ObjDesc = AcpiNsGetAttachedObject (Node);
1208     if (!ObjDesc)
1209     {
1210         /* No object, just exit */
1211
1212         return (AE_OK);
1213     }
1214
1215     /* Object is a Region */
1216
1217     if (ObjDesc->Region.SpaceId != SpaceId)
1218     {
1219         /* This region is for a different address space, just ignore it */
1220
1221         return (AE_OK);
1222     }
1223
1224     Status = AcpiEvExecuteRegMethod (ObjDesc, 1);
1225     return (Status);
1226 }
1227