1 /******************************************************************************
2  *
3  * Module Name: evrgnini- ACPI AddressSpace (OpRegion) init
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2019, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acnamesp.h"
48 #include "acinterp.h"
49 
50 #define _COMPONENT          ACPI_EVENTS
51         ACPI_MODULE_NAME    ("evrgnini")
52 
53 
54 /*******************************************************************************
55  *
56  * FUNCTION:    AcpiEvSystemMemoryRegionSetup
57  *
58  * PARAMETERS:  Handle              - Region we are interested in
59  *              Function            - Start or stop
60  *              HandlerContext      - Address space handler context
61  *              RegionContext       - Region specific context
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Setup a SystemMemory operation region
66  *
67  ******************************************************************************/
68 
69 ACPI_STATUS
70 AcpiEvSystemMemoryRegionSetup (
71     ACPI_HANDLE             Handle,
72     UINT32                  Function,
73     void                    *HandlerContext,
74     void                    **RegionContext)
75 {
76     ACPI_OPERAND_OBJECT     *RegionDesc = (ACPI_OPERAND_OBJECT *) Handle;
77     ACPI_MEM_SPACE_CONTEXT  *LocalRegionContext;
78 
79 
80     ACPI_FUNCTION_TRACE (EvSystemMemoryRegionSetup);
81 
82 
83     if (Function == ACPI_REGION_DEACTIVATE)
84     {
85         if (*RegionContext)
86         {
87             LocalRegionContext = (ACPI_MEM_SPACE_CONTEXT *) *RegionContext;
88 
89             /* Delete a cached mapping if present */
90 
91             if (LocalRegionContext->MappedLength)
92             {
93                 AcpiOsUnmapMemory (LocalRegionContext->MappedLogicalAddress,
94                     LocalRegionContext->MappedLength);
95             }
96             ACPI_FREE (LocalRegionContext);
97             *RegionContext = NULL;
98         }
99         return_ACPI_STATUS (AE_OK);
100     }
101 
102     /* Create a new context */
103 
104     LocalRegionContext = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_MEM_SPACE_CONTEXT));
105     if (!(LocalRegionContext))
106     {
107         return_ACPI_STATUS (AE_NO_MEMORY);
108     }
109 
110     /* Save the region length and address for use in the handler */
111 
112     LocalRegionContext->Length  = RegionDesc->Region.Length;
113     LocalRegionContext->Address = RegionDesc->Region.Address;
114 
115     *RegionContext = LocalRegionContext;
116     return_ACPI_STATUS (AE_OK);
117 }
118 
119 
120 /*******************************************************************************
121  *
122  * FUNCTION:    AcpiEvIoSpaceRegionSetup
123  *
124  * PARAMETERS:  Handle              - Region we are interested in
125  *              Function            - Start or stop
126  *              HandlerContext      - Address space handler context
127  *              RegionContext       - Region specific context
128  *
129  * RETURN:      Status
130  *
131  * DESCRIPTION: Setup a IO operation region
132  *
133  ******************************************************************************/
134 
135 ACPI_STATUS
136 AcpiEvIoSpaceRegionSetup (
137     ACPI_HANDLE             Handle,
138     UINT32                  Function,
139     void                    *HandlerContext,
140     void                    **RegionContext)
141 {
142     ACPI_FUNCTION_TRACE (EvIoSpaceRegionSetup);
143 
144 
145     if (Function == ACPI_REGION_DEACTIVATE)
146     {
147         *RegionContext = NULL;
148     }
149     else
150     {
151         *RegionContext = HandlerContext;
152     }
153 
154     return_ACPI_STATUS (AE_OK);
155 }
156 
157 
158 /*******************************************************************************
159  *
160  * FUNCTION:    AcpiEvPciConfigRegionSetup
161  *
162  * PARAMETERS:  Handle              - Region we are interested in
163  *              Function            - Start or stop
164  *              HandlerContext      - Address space handler context
165  *              RegionContext       - Region specific context
166  *
167  * RETURN:      Status
168  *
169  * DESCRIPTION: Setup a PCI_Config operation region
170  *
171  * MUTEX:       Assumes namespace is not locked
172  *
173  ******************************************************************************/
174 
175 ACPI_STATUS
176 AcpiEvPciConfigRegionSetup (
177     ACPI_HANDLE             Handle,
178     UINT32                  Function,
179     void                    *HandlerContext,
180     void                    **RegionContext)
181 {
182     ACPI_STATUS             Status = AE_OK;
183     UINT64                  PciValue;
184     ACPI_PCI_ID             *PciId = *RegionContext;
185     ACPI_OPERAND_OBJECT     *HandlerObj;
186     ACPI_NAMESPACE_NODE     *ParentNode;
187     ACPI_NAMESPACE_NODE     *PciRootNode;
188     ACPI_NAMESPACE_NODE     *PciDeviceNode;
189     ACPI_OPERAND_OBJECT     *RegionObj = (ACPI_OPERAND_OBJECT  *) Handle;
190 
191 
192     ACPI_FUNCTION_TRACE (EvPciConfigRegionSetup);
193 
194 
195     HandlerObj = RegionObj->Region.Handler;
196     if (!HandlerObj)
197     {
198         /*
199          * No installed handler. This shouldn't happen because the dispatch
200          * routine checks before we get here, but we check again just in case.
201          */
202         ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
203             "Attempting to init a region %p, with no handler\n", RegionObj));
204         return_ACPI_STATUS (AE_NOT_EXIST);
205     }
206 
207     *RegionContext = NULL;
208     if (Function == ACPI_REGION_DEACTIVATE)
209     {
210         if (PciId)
211         {
212             ACPI_FREE (PciId);
213         }
214         return_ACPI_STATUS (Status);
215     }
216 
217     ParentNode = RegionObj->Region.Node->Parent;
218 
219     /*
220      * Get the _SEG and _BBN values from the device upon which the handler
221      * is installed.
222      *
223      * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
224      * This is the device the handler has been registered to handle.
225      */
226 
227     /*
228      * If the AddressSpace.Node is still pointing to the root, we need
229      * to scan upward for a PCI Root bridge and re-associate the OpRegion
230      * handlers with that device.
231      */
232     if (HandlerObj->AddressSpace.Node == AcpiGbl_RootNode)
233     {
234         /* Start search from the parent object */
235 
236         PciRootNode = ParentNode;
237         while (PciRootNode != AcpiGbl_RootNode)
238         {
239             /* Get the _HID/_CID in order to detect a RootBridge */
240 
241             if (AcpiEvIsPciRootBridge (PciRootNode))
242             {
243                 /* Install a handler for this PCI root bridge */
244 
245                 Status = AcpiInstallAddressSpaceHandler (
246                     (ACPI_HANDLE) PciRootNode,
247                     ACPI_ADR_SPACE_PCI_CONFIG,
248                     ACPI_DEFAULT_HANDLER, NULL, NULL);
249                 if (ACPI_FAILURE (Status))
250                 {
251                     if (Status == AE_SAME_HANDLER)
252                     {
253                         /*
254                          * It is OK if the handler is already installed on the
255                          * root bridge. Still need to return a context object
256                          * for the new PCI_Config operation region, however.
257                          */
258                     }
259                     else
260                     {
261                         ACPI_EXCEPTION ((AE_INFO, Status,
262                             "Could not install PciConfig handler "
263                             "for Root Bridge %4.4s",
264                             AcpiUtGetNodeName (PciRootNode)));
265                     }
266                 }
267                 break;
268             }
269 
270             PciRootNode = PciRootNode->Parent;
271         }
272 
273         /* PCI root bridge not found, use namespace root node */
274     }
275     else
276     {
277         PciRootNode = HandlerObj->AddressSpace.Node;
278     }
279 
280     /*
281      * If this region is now initialized, we are done.
282      * (InstallAddressSpaceHandler could have initialized it)
283      */
284     if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
285     {
286         return_ACPI_STATUS (AE_OK);
287     }
288 
289     /* Region is still not initialized. Create a new context */
290 
291     PciId = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PCI_ID));
292     if (!PciId)
293     {
294         return_ACPI_STATUS (AE_NO_MEMORY);
295     }
296 
297     /*
298      * For PCI_Config space access, we need the segment, bus, device and
299      * function numbers. Acquire them here.
300      *
301      * Find the parent device object. (This allows the operation region to be
302      * within a subscope under the device, such as a control method.)
303      */
304     PciDeviceNode = RegionObj->Region.Node;
305     while (PciDeviceNode && (PciDeviceNode->Type != ACPI_TYPE_DEVICE))
306     {
307         PciDeviceNode = PciDeviceNode->Parent;
308     }
309 
310     if (!PciDeviceNode)
311     {
312         ACPI_FREE (PciId);
313         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
314     }
315 
316     /*
317      * Get the PCI device and function numbers from the _ADR object
318      * contained in the parent's scope.
319      */
320     Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR,
321         PciDeviceNode, &PciValue);
322 
323     /*
324      * The default is zero, and since the allocation above zeroed the data,
325      * just do nothing on failure.
326      */
327     if (ACPI_SUCCESS (Status))
328     {
329         PciId->Device   = ACPI_HIWORD (ACPI_LODWORD (PciValue));
330         PciId->Function = ACPI_LOWORD (ACPI_LODWORD (PciValue));
331     }
332 
333     /* The PCI segment number comes from the _SEG method */
334 
335     Status = AcpiUtEvaluateNumericObject (METHOD_NAME__SEG,
336         PciRootNode, &PciValue);
337     if (ACPI_SUCCESS (Status))
338     {
339         PciId->Segment = ACPI_LOWORD (PciValue);
340     }
341 
342     /* The PCI bus number comes from the _BBN method */
343 
344     Status = AcpiUtEvaluateNumericObject (METHOD_NAME__BBN,
345         PciRootNode, &PciValue);
346     if (ACPI_SUCCESS (Status))
347     {
348         PciId->Bus = ACPI_LOWORD (PciValue);
349     }
350 
351     /* Complete/update the PCI ID for this device */
352 
353     Status = AcpiHwDerivePciId (PciId, PciRootNode, RegionObj->Region.Node);
354     if (ACPI_FAILURE (Status))
355     {
356         ACPI_FREE (PciId);
357         return_ACPI_STATUS (Status);
358     }
359 
360     *RegionContext = PciId;
361     return_ACPI_STATUS (AE_OK);
362 }
363 
364 
365 /*******************************************************************************
366  *
367  * FUNCTION:    AcpiEvIsPciRootBridge
368  *
369  * PARAMETERS:  Node            - Device node being examined
370  *
371  * RETURN:      TRUE if device is a PCI/PCI-Express Root Bridge
372  *
373  * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
374  *              examining the _HID and _CID for the device.
375  *
376  ******************************************************************************/
377 
378 BOOLEAN
379 AcpiEvIsPciRootBridge (
380     ACPI_NAMESPACE_NODE     *Node)
381 {
382     ACPI_STATUS             Status;
383     ACPI_PNP_DEVICE_ID      *Hid;
384     ACPI_PNP_DEVICE_ID_LIST *Cid;
385     UINT32                  i;
386     BOOLEAN                 Match;
387 
388 
389     /* Get the _HID and check for a PCI Root Bridge */
390 
391     Status = AcpiUtExecute_HID (Node, &Hid);
392     if (ACPI_FAILURE (Status))
393     {
394         return (FALSE);
395     }
396 
397     Match = AcpiUtIsPciRootBridge (Hid->String);
398     ACPI_FREE (Hid);
399 
400     if (Match)
401     {
402         return (TRUE);
403     }
404 
405     /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
406 
407     Status = AcpiUtExecute_CID (Node, &Cid);
408     if (ACPI_FAILURE (Status))
409     {
410         return (FALSE);
411     }
412 
413     /* Check all _CIDs in the returned list */
414 
415     for (i = 0; i < Cid->Count; i++)
416     {
417         if (AcpiUtIsPciRootBridge (Cid->Ids[i].String))
418         {
419             ACPI_FREE (Cid);
420             return (TRUE);
421         }
422     }
423 
424     ACPI_FREE (Cid);
425     return (FALSE);
426 }
427 
428 
429 /*******************************************************************************
430  *
431  * FUNCTION:    AcpiEvPciBarRegionSetup
432  *
433  * PARAMETERS:  Handle              - Region we are interested in
434  *              Function            - Start or stop
435  *              HandlerContext      - Address space handler context
436  *              RegionContext       - Region specific context
437  *
438  * RETURN:      Status
439  *
440  * DESCRIPTION: Setup a PciBAR operation region
441  *
442  * MUTEX:       Assumes namespace is not locked
443  *
444  ******************************************************************************/
445 
446 ACPI_STATUS
447 AcpiEvPciBarRegionSetup (
448     ACPI_HANDLE             Handle,
449     UINT32                  Function,
450     void                    *HandlerContext,
451     void                    **RegionContext)
452 {
453     ACPI_FUNCTION_TRACE (EvPciBarRegionSetup);
454 
455 
456     return_ACPI_STATUS (AE_OK);
457 }
458 
459 
460 /*******************************************************************************
461  *
462  * FUNCTION:    AcpiEvCmosRegionSetup
463  *
464  * PARAMETERS:  Handle              - Region we are interested in
465  *              Function            - Start or stop
466  *              HandlerContext      - Address space handler context
467  *              RegionContext       - Region specific context
468  *
469  * RETURN:      Status
470  *
471  * DESCRIPTION: Setup a CMOS operation region
472  *
473  * MUTEX:       Assumes namespace is not locked
474  *
475  ******************************************************************************/
476 
477 ACPI_STATUS
478 AcpiEvCmosRegionSetup (
479     ACPI_HANDLE             Handle,
480     UINT32                  Function,
481     void                    *HandlerContext,
482     void                    **RegionContext)
483 {
484     ACPI_FUNCTION_TRACE (EvCmosRegionSetup);
485 
486 
487     return_ACPI_STATUS (AE_OK);
488 }
489 
490 
491 /*******************************************************************************
492  *
493  * FUNCTION:    AcpiEvDefaultRegionSetup
494  *
495  * PARAMETERS:  Handle              - Region we are interested in
496  *              Function            - Start or stop
497  *              HandlerContext      - Address space handler context
498  *              RegionContext       - Region specific context
499  *
500  * RETURN:      Status
501  *
502  * DESCRIPTION: Default region initialization
503  *
504  ******************************************************************************/
505 
506 ACPI_STATUS
507 AcpiEvDefaultRegionSetup (
508     ACPI_HANDLE             Handle,
509     UINT32                  Function,
510     void                    *HandlerContext,
511     void                    **RegionContext)
512 {
513     ACPI_FUNCTION_TRACE (EvDefaultRegionSetup);
514 
515 
516     if (Function == ACPI_REGION_DEACTIVATE)
517     {
518         *RegionContext = NULL;
519     }
520     else
521     {
522         *RegionContext = HandlerContext;
523     }
524 
525     return_ACPI_STATUS (AE_OK);
526 }
527 
528 
529 /*******************************************************************************
530  *
531  * FUNCTION:    AcpiEvInitializeRegion
532  *
533  * PARAMETERS:  RegionObj       - Region we are initializing
534  *
535  * RETURN:      Status
536  *
537  * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
538  *              for execution at a later time
539  *
540  *              Get the appropriate address space handler for a newly
541  *              created region.
542  *
543  *              This also performs address space specific initialization. For
544  *              example, PCI regions must have an _ADR object that contains
545  *              a PCI address in the scope of the definition. This address is
546  *              required to perform an access to PCI config space.
547  *
548  * MUTEX:       Interpreter should be unlocked, because we may run the _REG
549  *              method for this region.
550  *
551  * NOTE:        Possible incompliance:
552  *              There is a behavior conflict in automatic _REG execution:
553  *              1. When the interpreter is evaluating a method, we can only
554  *                 automatically run _REG for the following case:
555  *                   Method(_REG, 2) {}
556  *                   OperationRegion (OPR1, 0x80, 0x1000010, 0x4)
557  *              2. When the interpreter is loading a table, we can also
558  *                 automatically run _REG for the following case:
559  *                   OperationRegion (OPR1, 0x80, 0x1000010, 0x4)
560  *                   Method(_REG, 2) {}
561  *              Though this may not be compliant to the de-facto standard, the
562  *              logic is kept in order not to trigger regressions. And keeping
563  *              this logic should be taken care by the caller of this function.
564  *
565  ******************************************************************************/
566 
567 ACPI_STATUS
568 AcpiEvInitializeRegion (
569     ACPI_OPERAND_OBJECT     *RegionObj)
570 {
571     ACPI_OPERAND_OBJECT     *HandlerObj;
572     ACPI_OPERAND_OBJECT     *ObjDesc;
573     ACPI_ADR_SPACE_TYPE     SpaceId;
574     ACPI_NAMESPACE_NODE     *Node;
575 
576 
577     ACPI_FUNCTION_TRACE (EvInitializeRegion);
578 
579 
580     if (!RegionObj)
581     {
582         return_ACPI_STATUS (AE_BAD_PARAMETER);
583     }
584 
585     if (RegionObj->Common.Flags & AOPOBJ_OBJECT_INITIALIZED)
586     {
587         return_ACPI_STATUS (AE_OK);
588     }
589 
590     RegionObj->Common.Flags |= AOPOBJ_OBJECT_INITIALIZED;
591 
592     Node = RegionObj->Region.Node->Parent;
593     SpaceId = RegionObj->Region.SpaceId;
594 
595     /*
596      * The following loop depends upon the root Node having no parent
597      * ie: AcpiGbl_RootNode->Parent being set to NULL
598      */
599     while (Node)
600     {
601         /* Check to see if a handler exists */
602 
603         HandlerObj = NULL;
604         ObjDesc = AcpiNsGetAttachedObject (Node);
605         if (ObjDesc)
606         {
607             /* Can only be a handler if the object exists */
608 
609             switch (Node->Type)
610             {
611             case ACPI_TYPE_DEVICE:
612             case ACPI_TYPE_PROCESSOR:
613             case ACPI_TYPE_THERMAL:
614 
615                 HandlerObj = ObjDesc->CommonNotify.Handler;
616                 break;
617 
618             default:
619 
620                 /* Ignore other objects */
621 
622                 break;
623             }
624 
625             HandlerObj = AcpiEvFindRegionHandler (SpaceId, HandlerObj);
626             if (HandlerObj)
627             {
628                 /* Found correct handler */
629 
630                 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
631                     "Found handler %p for region %p in obj %p\n",
632                     HandlerObj, RegionObj, ObjDesc));
633 
634                 (void) AcpiEvAttachRegion (HandlerObj, RegionObj, FALSE);
635 
636                 /*
637                  * Tell all users that this region is usable by
638                  * running the _REG method
639                  */
640                 AcpiExExitInterpreter ();
641                 (void) AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_CONNECT);
642                 AcpiExEnterInterpreter ();
643                 return_ACPI_STATUS (AE_OK);
644             }
645         }
646 
647         /* This node does not have the handler we need; Pop up one level */
648 
649         Node = Node->Parent;
650     }
651 
652     /*
653      * If we get here, there is no handler for this region. This is not
654      * fatal because many regions get created before a handler is installed
655      * for said region.
656      */
657     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
658         "No handler for RegionType %s(%X) (RegionObj %p)\n",
659         AcpiUtGetRegionName (SpaceId), SpaceId, RegionObj));
660 
661     return_ACPI_STATUS (AE_OK);
662 }
663