1 /******************************************************************************
2  *
3  * Module Name: evregion - Operation Region support
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2021, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acevents.h"
155 #include "acnamesp.h"
156 #include "acinterp.h"
157 
158 #define _COMPONENT          ACPI_EVENTS
159         ACPI_MODULE_NAME    ("evregion")
160 
161 
162 extern UINT8        AcpiGbl_DefaultAddressSpaces[];
163 
164 /* Local prototypes */
165 
166 static void
167 AcpiEvExecuteOrphanRegMethod (
168     ACPI_NAMESPACE_NODE     *DeviceNode,
169     ACPI_ADR_SPACE_TYPE     SpaceId);
170 
171 
172 static ACPI_STATUS
173 AcpiEvRegRun (
174     ACPI_HANDLE             ObjHandle,
175     UINT32                  Level,
176     void                    *Context,
177     void                    **ReturnValue);
178 
179 
180 /*******************************************************************************
181  *
182  * FUNCTION:    AcpiEvInitializeOpRegions
183  *
184  * PARAMETERS:  None
185  *
186  * RETURN:      Status
187  *
188  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
189  *              an installed default region handler.
190  *
191  ******************************************************************************/
192 
193 ACPI_STATUS
194 AcpiEvInitializeOpRegions (
195     void)
196 {
197     ACPI_STATUS             Status;
198     UINT32                  i;
199 
200 
201     ACPI_FUNCTION_TRACE (EvInitializeOpRegions);
202 
203 
204     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
205     if (ACPI_FAILURE (Status))
206     {
207         return_ACPI_STATUS (Status);
208     }
209 
210     /* Run the _REG methods for OpRegions in each default address space */
211 
212     for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
213     {
214         /*
215          * Make sure the installed handler is the DEFAULT handler. If not the
216          * default, the _REG methods will have already been run (when the
217          * handler was installed)
218          */
219         if (AcpiEvHasDefaultHandler (AcpiGbl_RootNode,
220                AcpiGbl_DefaultAddressSpaces[i]))
221         {
222             AcpiEvExecuteRegMethods (AcpiGbl_RootNode,
223                 AcpiGbl_DefaultAddressSpaces[i], ACPI_REG_CONNECT);
224         }
225     }
226 
227     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
228     return_ACPI_STATUS (Status);
229 }
230 
231 
232 /*******************************************************************************
233  *
234  * FUNCTION:    AcpiEvAddressSpaceDispatch
235  *
236  * PARAMETERS:  RegionObj           - Internal region object
237  *              FieldObj            - Corresponding field. Can be NULL.
238  *              Function            - Read or Write operation
239  *              RegionOffset        - Where in the region to read or write
240  *              BitWidth            - Field width in bits (8, 16, 32, or 64)
241  *              Value               - Pointer to in or out value, must be
242  *                                    a full 64-bit integer
243  *
244  * RETURN:      Status
245  *
246  * DESCRIPTION: Dispatch an address space or operation region access to
247  *              a previously installed handler.
248  *
249  * NOTE: During early initialization, we always install the default region
250  * handlers for Memory, I/O and PCI_Config. This ensures that these operation
251  * region address spaces are always available as per the ACPI specification.
252  * This is especially needed in order to support the execution of
253  * module-level AML code during loading of the ACPI tables.
254  *
255  ******************************************************************************/
256 
257 ACPI_STATUS
258 AcpiEvAddressSpaceDispatch (
259     ACPI_OPERAND_OBJECT     *RegionObj,
260     ACPI_OPERAND_OBJECT     *FieldObj,
261     UINT32                  Function,
262     UINT32                  RegionOffset,
263     UINT32                  BitWidth,
264     UINT64                  *Value)
265 {
266     ACPI_STATUS             Status;
267     ACPI_ADR_SPACE_HANDLER  Handler;
268     ACPI_ADR_SPACE_SETUP    RegionSetup;
269     ACPI_OPERAND_OBJECT     *HandlerDesc;
270     ACPI_OPERAND_OBJECT     *RegionObj2;
271     void                    *RegionContext = NULL;
272     ACPI_CONNECTION_INFO    *Context;
273     ACPI_MUTEX              ContextMutex;
274     BOOLEAN                 ContextLocked;
275     ACPI_PHYSICAL_ADDRESS   Address;
276 
277 
278     ACPI_FUNCTION_TRACE (EvAddressSpaceDispatch);
279 
280 
281     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
282     if (!RegionObj2)
283     {
284         return_ACPI_STATUS (AE_NOT_EXIST);
285     }
286 
287     /* Ensure that there is a handler associated with this region */
288 
289     HandlerDesc = RegionObj->Region.Handler;
290     if (!HandlerDesc)
291     {
292         ACPI_ERROR ((AE_INFO,
293             "No handler for Region [%4.4s] (%p) [%s]",
294             AcpiUtGetNodeName (RegionObj->Region.Node),
295             RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
296 
297         return_ACPI_STATUS (AE_NOT_EXIST);
298     }
299 
300     Context = HandlerDesc->AddressSpace.Context;
301     ContextMutex = HandlerDesc->AddressSpace.ContextMutex;
302     ContextLocked = FALSE;
303 
304     /*
305      * It may be the case that the region has never been initialized.
306      * Some types of regions require special init code
307      */
308     if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
309     {
310         /* This region has not been initialized yet, do it */
311 
312         RegionSetup = HandlerDesc->AddressSpace.Setup;
313         if (!RegionSetup)
314         {
315             /* No initialization routine, exit with error */
316 
317             ACPI_ERROR ((AE_INFO,
318                 "No init routine for region(%p) [%s]",
319                 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
320             return_ACPI_STATUS (AE_NOT_EXIST);
321         }
322 
323         /*
324          * We must exit the interpreter because the region setup will
325          * potentially execute control methods (for example, the _REG method
326          * for this region)
327          */
328         AcpiExExitInterpreter ();
329 
330         Status = RegionSetup (RegionObj, ACPI_REGION_ACTIVATE,
331             Context, &RegionContext);
332 
333         /* Re-enter the interpreter */
334 
335         AcpiExEnterInterpreter ();
336 
337         /* Check for failure of the Region Setup */
338 
339         if (ACPI_FAILURE (Status))
340         {
341             ACPI_EXCEPTION ((AE_INFO, Status,
342                 "During region initialization: [%s]",
343                 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
344             return_ACPI_STATUS (Status);
345         }
346 
347         /* Region initialization may have been completed by RegionSetup */
348 
349         if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
350         {
351             RegionObj->Region.Flags |= AOPOBJ_SETUP_COMPLETE;
352 
353             /*
354              * Save the returned context for use in all accesses to
355              * the handler for this particular region
356              */
357             if (!(RegionObj2->Extra.RegionContext))
358             {
359                 RegionObj2->Extra.RegionContext = RegionContext;
360             }
361         }
362     }
363 
364     /* We have everything we need, we can invoke the address space handler */
365 
366     Handler = HandlerDesc->AddressSpace.Handler;
367     Address = (RegionObj->Region.Address + RegionOffset);
368 
369     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
370         "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
371         &RegionObj->Region.Handler->AddressSpace, Handler,
372         ACPI_FORMAT_UINT64 (Address),
373         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
374 
375     if (!(HandlerDesc->AddressSpace.HandlerFlags &
376         ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
377     {
378         /*
379          * For handlers other than the default (supplied) handlers, we must
380          * exit the interpreter because the handler *might* block -- we don't
381          * know what it will do, so we can't hold the lock on the interpreter.
382          */
383         AcpiExExitInterpreter();
384     }
385 
386     /*
387      * Special handling for GenericSerialBus and GeneralPurposeIo:
388      * There are three extra parameters that must be passed to the
389      * handler via the context:
390      *   1) Connection buffer, a resource template from Connection() op
391      *   2) Length of the above buffer
392      *   3) Actual access length from the AccessAs() op
393      *
394      * Since we pass these extra parameters via the context, which is
395      * shared between threads, we must lock the context to avoid these
396      * parameters being changed from another thread before the handler
397      * has completed running.
398      *
399      * In addition, for GeneralPurposeIo, the Address and BitWidth fields
400      * are defined as follows:
401      *   1) Address is the pin number index of the field (bit offset from
402      *      the previous Connection)
403      *   2) BitWidth is the actual bit length of the field (number of pins)
404      */
405     if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
406          RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO) &&
407         Context &&
408         FieldObj)
409     {
410 
411         Status = AcpiOsAcquireMutex (ContextMutex, ACPI_WAIT_FOREVER);
412         if (ACPI_FAILURE (Status))
413         {
414             goto ReEnterInterpreter;
415         }
416 
417         ContextLocked = TRUE;
418 
419         /* Get the Connection (ResourceTemplate) buffer */
420 
421         Context->Connection = FieldObj->Field.ResourceBuffer;
422         Context->Length = FieldObj->Field.ResourceLength;
423         Context->AccessLength = FieldObj->Field.AccessLength;
424 
425         if (RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO)
426         {
427             Address = FieldObj->Field.PinNumberIndex;
428             BitWidth = FieldObj->Field.BitLength;
429         }
430     }
431 
432     /* Call the handler */
433 
434     Status = Handler (Function, Address, BitWidth, Value, Context,
435         RegionObj2->Extra.RegionContext);
436 
437     if (ContextLocked)
438     {
439         AcpiOsReleaseMutex (ContextMutex);
440     }
441 
442     if (ACPI_FAILURE (Status))
443     {
444         ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
445             AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
446 
447         /*
448          * Special case for an EC timeout. These are seen so frequently
449          * that an additional error message is helpful
450          */
451         if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_EC) &&
452             (Status == AE_TIME))
453         {
454             ACPI_ERROR ((AE_INFO,
455                 "Timeout from EC hardware or EC device driver"));
456         }
457     }
458 
459 ReEnterInterpreter:
460     if (!(HandlerDesc->AddressSpace.HandlerFlags &
461         ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
462     {
463         /*
464          * We just returned from a non-default handler, we must re-enter the
465          * interpreter
466          */
467         AcpiExEnterInterpreter ();
468     }
469 
470     return_ACPI_STATUS (Status);
471 }
472 
473 
474 /*******************************************************************************
475  *
476  * FUNCTION:    AcpiEvDetachRegion
477  *
478  * PARAMETERS:  RegionObj           - Region Object
479  *              AcpiNsIsLocked      - Namespace Region Already Locked?
480  *
481  * RETURN:      None
482  *
483  * DESCRIPTION: Break the association between the handler and the region
484  *              this is a two way association.
485  *
486  ******************************************************************************/
487 
488 void
489 AcpiEvDetachRegion (
490     ACPI_OPERAND_OBJECT     *RegionObj,
491     BOOLEAN                 AcpiNsIsLocked)
492 {
493     ACPI_OPERAND_OBJECT     *HandlerObj;
494     ACPI_OPERAND_OBJECT     *ObjDesc;
495     ACPI_OPERAND_OBJECT     *StartDesc;
496     ACPI_OPERAND_OBJECT     **LastObjPtr;
497     ACPI_ADR_SPACE_SETUP    RegionSetup;
498     void                    **RegionContext;
499     ACPI_OPERAND_OBJECT     *RegionObj2;
500     ACPI_STATUS             Status;
501 
502 
503     ACPI_FUNCTION_TRACE (EvDetachRegion);
504 
505 
506     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
507     if (!RegionObj2)
508     {
509         return_VOID;
510     }
511     RegionContext = &RegionObj2->Extra.RegionContext;
512 
513     /* Get the address handler from the region object */
514 
515     HandlerObj = RegionObj->Region.Handler;
516     if (!HandlerObj)
517     {
518         /* This region has no handler, all done */
519 
520         return_VOID;
521     }
522 
523     /* Find this region in the handler's list */
524 
525     ObjDesc = HandlerObj->AddressSpace.RegionList;
526     StartDesc = ObjDesc;
527     LastObjPtr = &HandlerObj->AddressSpace.RegionList;
528 
529     while (ObjDesc)
530     {
531         /* Is this the correct Region? */
532 
533         if (ObjDesc == RegionObj)
534         {
535             ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
536                 "Removing Region %p from address handler %p\n",
537                 RegionObj, HandlerObj));
538 
539             /* This is it, remove it from the handler's list */
540 
541             *LastObjPtr = ObjDesc->Region.Next;
542             ObjDesc->Region.Next = NULL;        /* Must clear field */
543 
544             if (AcpiNsIsLocked)
545             {
546                 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
547                 if (ACPI_FAILURE (Status))
548                 {
549                     return_VOID;
550                 }
551             }
552 
553             /* Now stop region accesses by executing the _REG method */
554 
555             Status = AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_DISCONNECT);
556             if (ACPI_FAILURE (Status))
557             {
558                 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
559                     AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
560             }
561 
562             if (AcpiNsIsLocked)
563             {
564                 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
565                 if (ACPI_FAILURE (Status))
566                 {
567                     return_VOID;
568                 }
569             }
570 
571             /*
572              * If the region has been activated, call the setup handler with
573              * the deactivate notification
574              */
575             if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
576             {
577                 RegionSetup = HandlerObj->AddressSpace.Setup;
578                 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
579                     HandlerObj->AddressSpace.Context, RegionContext);
580 
581                 /*
582                  * RegionContext should have been released by the deactivate
583                  * operation. We don't need access to it anymore here.
584                  */
585                 if (RegionContext)
586                 {
587                     *RegionContext = NULL;
588                 }
589 
590                 /* Init routine may fail, Just ignore errors */
591 
592                 if (ACPI_FAILURE (Status))
593                 {
594                     ACPI_EXCEPTION ((AE_INFO, Status,
595                         "from region handler - deactivate, [%s]",
596                         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
597                 }
598 
599                 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
600             }
601 
602             /*
603              * Remove handler reference in the region
604              *
605              * NOTE: this doesn't mean that the region goes away, the region
606              * is just inaccessible as indicated to the _REG method
607              *
608              * If the region is on the handler's list, this must be the
609              * region's handler
610              */
611             RegionObj->Region.Handler = NULL;
612             AcpiUtRemoveReference (HandlerObj);
613 
614             return_VOID;
615         }
616 
617         /* Walk the linked list of handlers */
618 
619         LastObjPtr = &ObjDesc->Region.Next;
620         ObjDesc = ObjDesc->Region.Next;
621 
622         /* Prevent infinite loop if list is corrupted */
623 
624         if (ObjDesc == StartDesc)
625         {
626             ACPI_ERROR ((AE_INFO,
627                 "Circular handler list in region object %p",
628                 RegionObj));
629             return_VOID;
630         }
631     }
632 
633     /* If we get here, the region was not in the handler's region list */
634 
635     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
636         "Cannot remove region %p from address handler %p\n",
637         RegionObj, HandlerObj));
638 
639     return_VOID;
640 }
641 
642 
643 /*******************************************************************************
644  *
645  * FUNCTION:    AcpiEvAttachRegion
646  *
647  * PARAMETERS:  HandlerObj          - Handler Object
648  *              RegionObj           - Region Object
649  *              AcpiNsIsLocked      - Namespace Region Already Locked?
650  *
651  * RETURN:      None
652  *
653  * DESCRIPTION: Create the association between the handler and the region
654  *              this is a two way association.
655  *
656  ******************************************************************************/
657 
658 ACPI_STATUS
659 AcpiEvAttachRegion (
660     ACPI_OPERAND_OBJECT     *HandlerObj,
661     ACPI_OPERAND_OBJECT     *RegionObj,
662     BOOLEAN                 AcpiNsIsLocked)
663 {
664 
665     ACPI_FUNCTION_TRACE (EvAttachRegion);
666 
667 
668     /* Install the region's handler */
669 
670     if (RegionObj->Region.Handler)
671     {
672         return_ACPI_STATUS (AE_ALREADY_EXISTS);
673     }
674 
675     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
676         "Adding Region [%4.4s] %p to address handler %p [%s]\n",
677         AcpiUtGetNodeName (RegionObj->Region.Node),
678         RegionObj, HandlerObj,
679         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
680 
681     /* Link this region to the front of the handler's list */
682 
683     RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
684     HandlerObj->AddressSpace.RegionList = RegionObj;
685     RegionObj->Region.Handler = HandlerObj;
686     AcpiUtAddReference (HandlerObj);
687 
688     return_ACPI_STATUS (AE_OK);
689 }
690 
691 
692 /*******************************************************************************
693  *
694  * FUNCTION:    AcpiEvExecuteRegMethod
695  *
696  * PARAMETERS:  RegionObj           - Region object
697  *              Function            - Passed to _REG: On (1) or Off (0)
698  *
699  * RETURN:      Status
700  *
701  * DESCRIPTION: Execute _REG method for a region
702  *
703  ******************************************************************************/
704 
705 ACPI_STATUS
706 AcpiEvExecuteRegMethod (
707     ACPI_OPERAND_OBJECT     *RegionObj,
708     UINT32                  Function)
709 {
710     ACPI_EVALUATE_INFO      *Info;
711     ACPI_OPERAND_OBJECT     *Args[3];
712     ACPI_OPERAND_OBJECT     *RegionObj2;
713     const ACPI_NAME         *RegNamePtr = ACPI_CAST_PTR (ACPI_NAME, METHOD_NAME__REG);
714     ACPI_NAMESPACE_NODE     *MethodNode;
715     ACPI_NAMESPACE_NODE     *Node;
716     ACPI_STATUS             Status;
717 
718 
719     ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
720 
721 
722     if (!AcpiGbl_NamespaceInitialized ||
723         RegionObj->Region.Handler == NULL)
724     {
725         return_ACPI_STATUS (AE_OK);
726     }
727 
728     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
729     if (!RegionObj2)
730     {
731         return_ACPI_STATUS (AE_NOT_EXIST);
732     }
733 
734     /*
735      * Find any "_REG" method associated with this region definition.
736      * The method should always be updated as this function may be
737      * invoked after a namespace change.
738      */
739     Node = RegionObj->Region.Node->Parent;
740     Status = AcpiNsSearchOneScope (
741         *RegNamePtr, Node, ACPI_TYPE_METHOD, &MethodNode);
742     if (ACPI_SUCCESS (Status))
743     {
744         /*
745          * The _REG method is optional and there can be only one per
746          * region definition. This will be executed when the handler is
747          * attached or removed.
748          */
749         RegionObj2->Extra.Method_REG = MethodNode;
750     }
751     if (RegionObj2->Extra.Method_REG == NULL)
752     {
753         return_ACPI_STATUS (AE_OK);
754     }
755 
756     /* _REG(DISCONNECT) should be paired with _REG(CONNECT) */
757 
758     if ((Function == ACPI_REG_CONNECT &&
759         RegionObj->Common.Flags & AOPOBJ_REG_CONNECTED) ||
760         (Function == ACPI_REG_DISCONNECT &&
761          !(RegionObj->Common.Flags & AOPOBJ_REG_CONNECTED)))
762     {
763         return_ACPI_STATUS (AE_OK);
764     }
765 
766     /* Allocate and initialize the evaluation information block */
767 
768     Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
769     if (!Info)
770     {
771         return_ACPI_STATUS (AE_NO_MEMORY);
772     }
773 
774     Info->PrefixNode = RegionObj2->Extra.Method_REG;
775     Info->RelativePathname = NULL;
776     Info->Parameters = Args;
777     Info->Flags = ACPI_IGNORE_RETURN_VALUE;
778 
779     /*
780      * The _REG method has two arguments:
781      *
782      * Arg0 - Integer:
783      *  Operation region space ID Same value as RegionObj->Region.SpaceId
784      *
785      * Arg1 - Integer:
786      *  connection status 1 for connecting the handler, 0 for disconnecting
787      *  the handler (Passed as a parameter)
788      */
789     Args[0] = AcpiUtCreateIntegerObject ((UINT64) RegionObj->Region.SpaceId);
790     if (!Args[0])
791     {
792         Status = AE_NO_MEMORY;
793         goto Cleanup1;
794     }
795 
796     Args[1] = AcpiUtCreateIntegerObject ((UINT64) Function);
797     if (!Args[1])
798     {
799         Status = AE_NO_MEMORY;
800         goto Cleanup2;
801     }
802 
803     Args[2] = NULL; /* Terminate list */
804 
805     /* Execute the method, no return value */
806 
807     ACPI_DEBUG_EXEC (
808         AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
809 
810     Status = AcpiNsEvaluate (Info);
811     AcpiUtRemoveReference (Args[1]);
812 
813     if (ACPI_FAILURE (Status))
814     {
815         goto Cleanup2;
816     }
817 
818     if (Function == ACPI_REG_CONNECT)
819     {
820         RegionObj->Common.Flags |= AOPOBJ_REG_CONNECTED;
821     }
822     else
823     {
824         RegionObj->Common.Flags &= ~AOPOBJ_REG_CONNECTED;
825     }
826 
827 Cleanup2:
828     AcpiUtRemoveReference (Args[0]);
829 
830 Cleanup1:
831     ACPI_FREE (Info);
832     return_ACPI_STATUS (Status);
833 }
834 
835 
836 /*******************************************************************************
837  *
838  * FUNCTION:    AcpiEvExecuteRegMethods
839  *
840  * PARAMETERS:  Node            - Namespace node for the device
841  *              SpaceId         - The address space ID
842  *              Function        - Passed to _REG: On (1) or Off (0)
843  *
844  * RETURN:      None
845  *
846  * DESCRIPTION: Run all _REG methods for the input Space ID;
847  *              Note: assumes namespace is locked, or system init time.
848  *
849  ******************************************************************************/
850 
851 void
852 AcpiEvExecuteRegMethods (
853     ACPI_NAMESPACE_NODE     *Node,
854     ACPI_ADR_SPACE_TYPE     SpaceId,
855     UINT32                  Function)
856 {
857     ACPI_REG_WALK_INFO      Info;
858 
859 
860     ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
861 
862     /*
863      * These address spaces do not need a call to _REG, since the ACPI
864      * specification defines them as: "must always be accessible". Since
865      * they never change state (never become unavailable), no need to ever
866      * call _REG on them. Also, a DataTable is not a "real" address space,
867      * so do not call _REG. September 2018.
868      */
869     if ((SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY) ||
870         (SpaceId == ACPI_ADR_SPACE_SYSTEM_IO) ||
871         (SpaceId == ACPI_ADR_SPACE_DATA_TABLE))
872     {
873         return_VOID;
874     }
875 
876     Info.SpaceId = SpaceId;
877     Info.Function = Function;
878     Info.RegRunCount = 0;
879 
880     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_NAMES,
881         "    Running _REG methods for SpaceId %s\n",
882         AcpiUtGetRegionName (Info.SpaceId)));
883 
884     /*
885      * Run all _REG methods for all Operation Regions for this space ID. This
886      * is a separate walk in order to handle any interdependencies between
887      * regions and _REG methods. (i.e. handlers must be installed for all
888      * regions of this Space ID before we can run any _REG methods)
889      */
890     (void) AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
891         ACPI_NS_WALK_UNLOCK, AcpiEvRegRun, NULL, &Info, NULL);
892 
893     /*
894      * Special case for EC and GPIO: handle "orphan" _REG methods with
895      * no region.
896      */
897     if (SpaceId == ACPI_ADR_SPACE_EC || SpaceId == ACPI_ADR_SPACE_GPIO)
898     {
899         AcpiEvExecuteOrphanRegMethod (Node, SpaceId);
900     }
901 
902     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_NAMES,
903         "    Executed %u _REG methods for SpaceId %s\n",
904         Info.RegRunCount, AcpiUtGetRegionName (Info.SpaceId)));
905 
906     return_VOID;
907 }
908 
909 
910 /*******************************************************************************
911  *
912  * FUNCTION:    AcpiEvRegRun
913  *
914  * PARAMETERS:  WalkNamespace callback
915  *
916  * DESCRIPTION: Run _REG method for region objects of the requested spaceID
917  *
918  ******************************************************************************/
919 
920 static ACPI_STATUS
921 AcpiEvRegRun (
922     ACPI_HANDLE             ObjHandle,
923     UINT32                  Level,
924     void                    *Context,
925     void                    **ReturnValue)
926 {
927     ACPI_OPERAND_OBJECT     *ObjDesc;
928     ACPI_NAMESPACE_NODE     *Node;
929     ACPI_STATUS             Status;
930     ACPI_REG_WALK_INFO      *Info;
931 
932 
933     Info = ACPI_CAST_PTR (ACPI_REG_WALK_INFO, Context);
934 
935     /* Convert and validate the device handle */
936 
937     Node = AcpiNsValidateHandle (ObjHandle);
938     if (!Node)
939     {
940         return (AE_BAD_PARAMETER);
941     }
942 
943     /*
944      * We only care about regions and objects that are allowed to have
945      * address space handlers
946      */
947     if ((Node->Type != ACPI_TYPE_REGION) &&
948         (Node != AcpiGbl_RootNode))
949     {
950         return (AE_OK);
951     }
952 
953     /* Check for an existing internal object */
954 
955     ObjDesc = AcpiNsGetAttachedObject (Node);
956     if (!ObjDesc)
957     {
958         /* No object, just exit */
959 
960         return (AE_OK);
961     }
962 
963     /* Object is a Region */
964 
965     if (ObjDesc->Region.SpaceId != Info->SpaceId)
966     {
967         /* This region is for a different address space, just ignore it */
968 
969         return (AE_OK);
970     }
971 
972     Info->RegRunCount++;
973     Status = AcpiEvExecuteRegMethod (ObjDesc, Info->Function);
974     return (Status);
975 }
976 
977 
978 /*******************************************************************************
979  *
980  * FUNCTION:    AcpiEvExecuteOrphanRegMethod
981  *
982  * PARAMETERS:  DeviceNode          - Namespace node for an ACPI device
983  *              SpaceId             - The address space ID
984  *
985  * RETURN:      None
986  *
987  * DESCRIPTION: Execute an "orphan" _REG method that appears under an ACPI
988  *              device. This is a _REG method that has no corresponding region
989  *              within the device's scope. ACPI tables depending on these
990  *              "orphan" _REG methods have been seen for both EC and GPIO
991  *              Operation Regions. Presumably the Windows ACPI implementation
992  *              always calls the _REG method independent of the presence of
993  *              an actual Operation Region with the correct address space ID.
994  *
995  *  MUTEX:      Assumes the namespace is locked
996  *
997  ******************************************************************************/
998 
999 static void
1000 AcpiEvExecuteOrphanRegMethod (
1001     ACPI_NAMESPACE_NODE     *DeviceNode,
1002     ACPI_ADR_SPACE_TYPE     SpaceId)
1003 {
1004     ACPI_HANDLE             RegMethod;
1005     ACPI_NAMESPACE_NODE     *NextNode;
1006     ACPI_STATUS             Status;
1007     ACPI_OBJECT_LIST        Args;
1008     ACPI_OBJECT             Objects[2];
1009 
1010 
1011     ACPI_FUNCTION_TRACE (EvExecuteOrphanRegMethod);
1012 
1013 
1014     if (!DeviceNode)
1015     {
1016         return_VOID;
1017     }
1018 
1019     /* Namespace is currently locked, must release */
1020 
1021     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1022 
1023     /* Get a handle to a _REG method immediately under the EC device */
1024 
1025     Status = AcpiGetHandle (DeviceNode, METHOD_NAME__REG, &RegMethod);
1026     if (ACPI_FAILURE (Status))
1027     {
1028         goto Exit; /* There is no _REG method present */
1029     }
1030 
1031     /*
1032      * Execute the _REG method only if there is no Operation Region in
1033      * this scope with the Embedded Controller space ID. Otherwise, it
1034      * will already have been executed. Note, this allows for Regions
1035      * with other space IDs to be present; but the code below will then
1036      * execute the _REG method with the EmbeddedControl SpaceID argument.
1037      */
1038     NextNode = AcpiNsGetNextNode (DeviceNode, NULL);
1039     while (NextNode)
1040     {
1041         if ((NextNode->Type == ACPI_TYPE_REGION) &&
1042             (NextNode->Object) &&
1043             (NextNode->Object->Region.SpaceId == SpaceId))
1044         {
1045             goto Exit; /* Do not execute the _REG */
1046         }
1047 
1048         NextNode = AcpiNsGetNextNode (DeviceNode, NextNode);
1049     }
1050 
1051     /* Evaluate the _REG(SpaceId,Connect) method */
1052 
1053     Args.Count = 2;
1054     Args.Pointer = Objects;
1055     Objects[0].Type = ACPI_TYPE_INTEGER;
1056     Objects[0].Integer.Value = SpaceId;
1057     Objects[1].Type = ACPI_TYPE_INTEGER;
1058     Objects[1].Integer.Value = ACPI_REG_CONNECT;
1059 
1060     (void) AcpiEvaluateObject (RegMethod, NULL, &Args, NULL);
1061 
1062 Exit:
1063     /* We ignore all errors from above, don't care */
1064 
1065     (void) AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1066     return_VOID;
1067 }
1068