xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/events/evhandler.c (revision d0ac609964842f8cdb6d54b3c539c6c15293e172)
1 /******************************************************************************
2  *
3  * Module Name: evhandler - Support for Address Space handlers
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2015, 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 #include "acpi.h"
117 #include "accommon.h"
118 #include "acevents.h"
119 #include "acnamesp.h"
120 #include "acinterp.h"
121 
122 #define _COMPONENT          ACPI_EVENTS
123         ACPI_MODULE_NAME    ("evhandler")
124 
125 
126 /* Local prototypes */
127 
128 static ACPI_STATUS
129 AcpiEvInstallHandler (
130     ACPI_HANDLE             ObjHandle,
131     UINT32                  Level,
132     void                    *Context,
133     void                    **ReturnValue);
134 
135 /* These are the address spaces that will get default handlers */
136 
137 UINT8        AcpiGbl_DefaultAddressSpaces[ACPI_NUM_DEFAULT_SPACES] =
138 {
139     ACPI_ADR_SPACE_SYSTEM_MEMORY,
140     ACPI_ADR_SPACE_SYSTEM_IO,
141     ACPI_ADR_SPACE_PCI_CONFIG,
142     ACPI_ADR_SPACE_DATA_TABLE
143 };
144 
145 
146 /*******************************************************************************
147  *
148  * FUNCTION:    AcpiEvInstallRegionHandlers
149  *
150  * PARAMETERS:  None
151  *
152  * RETURN:      Status
153  *
154  * DESCRIPTION: Installs the core subsystem default address space handlers.
155  *
156  ******************************************************************************/
157 
158 ACPI_STATUS
159 AcpiEvInstallRegionHandlers (
160     void)
161 {
162     ACPI_STATUS             Status;
163     UINT32                  i;
164 
165 
166     ACPI_FUNCTION_TRACE (EvInstallRegionHandlers);
167 
168 
169     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
170     if (ACPI_FAILURE (Status))
171     {
172         return_ACPI_STATUS (Status);
173     }
174 
175     /*
176      * All address spaces (PCI Config, EC, SMBus) are scope dependent and
177      * registration must occur for a specific device.
178      *
179      * In the case of the system memory and IO address spaces there is
180      * currently no device associated with the address space. For these we
181      * use the root.
182      *
183      * We install the default PCI config space handler at the root so that
184      * this space is immediately available even though the we have not
185      * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
186      * specification which states that the PCI config space must be always
187      * available -- even though we are nowhere near ready to find the PCI root
188      * buses at this point.
189      *
190      * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
191      * has already been installed (via AcpiInstallAddressSpaceHandler).
192      * Similar for AE_SAME_HANDLER.
193      */
194     for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
195     {
196         Status = AcpiEvInstallSpaceHandler (AcpiGbl_RootNode,
197                     AcpiGbl_DefaultAddressSpaces[i],
198                     ACPI_DEFAULT_HANDLER, NULL, NULL);
199         switch (Status)
200         {
201         case AE_OK:
202         case AE_SAME_HANDLER:
203         case AE_ALREADY_EXISTS:
204 
205             /* These exceptions are all OK */
206 
207             Status = AE_OK;
208             break;
209 
210         default:
211 
212             goto UnlockAndExit;
213         }
214     }
215 
216 UnlockAndExit:
217     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
218     return_ACPI_STATUS (Status);
219 }
220 
221 
222 /*******************************************************************************
223  *
224  * FUNCTION:    AcpiEvHasDefaultHandler
225  *
226  * PARAMETERS:  Node                - Namespace node for the device
227  *              SpaceId             - The address space ID
228  *
229  * RETURN:      TRUE if default handler is installed, FALSE otherwise
230  *
231  * DESCRIPTION: Check if the default handler is installed for the requested
232  *              space ID.
233  *
234  ******************************************************************************/
235 
236 BOOLEAN
237 AcpiEvHasDefaultHandler (
238     ACPI_NAMESPACE_NODE     *Node,
239     ACPI_ADR_SPACE_TYPE     SpaceId)
240 {
241     ACPI_OPERAND_OBJECT     *ObjDesc;
242     ACPI_OPERAND_OBJECT     *HandlerObj;
243 
244 
245     /* Must have an existing internal object */
246 
247     ObjDesc = AcpiNsGetAttachedObject (Node);
248     if (ObjDesc)
249     {
250         HandlerObj = ObjDesc->Device.Handler;
251 
252         /* Walk the linked list of handlers for this object */
253 
254         while (HandlerObj)
255         {
256             if (HandlerObj->AddressSpace.SpaceId == SpaceId)
257             {
258                 if (HandlerObj->AddressSpace.HandlerFlags &
259                         ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
260                 {
261                     return (TRUE);
262                 }
263             }
264 
265             HandlerObj = HandlerObj->AddressSpace.Next;
266         }
267     }
268 
269     return (FALSE);
270 }
271 
272 
273 /*******************************************************************************
274  *
275  * FUNCTION:    AcpiEvInstallHandler
276  *
277  * PARAMETERS:  WalkNamespace callback
278  *
279  * DESCRIPTION: This routine installs an address handler into objects that are
280  *              of type Region or Device.
281  *
282  *              If the Object is a Device, and the device has a handler of
283  *              the same type then the search is terminated in that branch.
284  *
285  *              This is because the existing handler is closer in proximity
286  *              to any more regions than the one we are trying to install.
287  *
288  ******************************************************************************/
289 
290 static ACPI_STATUS
291 AcpiEvInstallHandler (
292     ACPI_HANDLE             ObjHandle,
293     UINT32                  Level,
294     void                    *Context,
295     void                    **ReturnValue)
296 {
297     ACPI_OPERAND_OBJECT     *HandlerObj;
298     ACPI_OPERAND_OBJECT     *NextHandlerObj;
299     ACPI_OPERAND_OBJECT     *ObjDesc;
300     ACPI_NAMESPACE_NODE     *Node;
301     ACPI_STATUS             Status;
302 
303 
304     ACPI_FUNCTION_NAME (EvInstallHandler);
305 
306 
307     HandlerObj = (ACPI_OPERAND_OBJECT  *) Context;
308 
309     /* Parameter validation */
310 
311     if (!HandlerObj)
312     {
313         return (AE_OK);
314     }
315 
316     /* Convert and validate the device handle */
317 
318     Node = AcpiNsValidateHandle (ObjHandle);
319     if (!Node)
320     {
321         return (AE_BAD_PARAMETER);
322     }
323 
324     /*
325      * We only care about regions and objects that are allowed to have
326      * address space handlers
327      */
328     if ((Node->Type != ACPI_TYPE_DEVICE) &&
329         (Node->Type != ACPI_TYPE_REGION) &&
330         (Node != AcpiGbl_RootNode))
331     {
332         return (AE_OK);
333     }
334 
335     /* Check for an existing internal object */
336 
337     ObjDesc = AcpiNsGetAttachedObject (Node);
338     if (!ObjDesc)
339     {
340         /* No object, just exit */
341 
342         return (AE_OK);
343     }
344 
345     /* Devices are handled different than regions */
346 
347     if (ObjDesc->Common.Type == ACPI_TYPE_DEVICE)
348     {
349         /* Check if this Device already has a handler for this address space */
350 
351         NextHandlerObj = ObjDesc->Device.Handler;
352         while (NextHandlerObj)
353         {
354             /* Found a handler, is it for the same address space? */
355 
356             if (NextHandlerObj->AddressSpace.SpaceId ==
357                     HandlerObj->AddressSpace.SpaceId)
358             {
359                 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
360                     "Found handler for region [%s] in device %p(%p) "
361                     "handler %p\n",
362                     AcpiUtGetRegionName (HandlerObj->AddressSpace.SpaceId),
363                     ObjDesc, NextHandlerObj, HandlerObj));
364 
365                 /*
366                  * Since the object we found it on was a device, then it
367                  * means that someone has already installed a handler for
368                  * the branch of the namespace from this device on. Just
369                  * bail out telling the walk routine to not traverse this
370                  * branch. This preserves the scoping rule for handlers.
371                  */
372                 return (AE_CTRL_DEPTH);
373             }
374 
375             /* Walk the linked list of handlers attached to this device */
376 
377             NextHandlerObj = NextHandlerObj->AddressSpace.Next;
378         }
379 
380         /*
381          * As long as the device didn't have a handler for this space we
382          * don't care about it. We just ignore it and proceed.
383          */
384         return (AE_OK);
385     }
386 
387     /* Object is a Region */
388 
389     if (ObjDesc->Region.SpaceId != HandlerObj->AddressSpace.SpaceId)
390     {
391         /* This region is for a different address space, just ignore it */
392 
393         return (AE_OK);
394     }
395 
396     /*
397      * Now we have a region and it is for the handler's address space type.
398      *
399      * First disconnect region for any previous handler (if any)
400      */
401     AcpiEvDetachRegion (ObjDesc, FALSE);
402 
403     /* Connect the region to the new handler */
404 
405     Status = AcpiEvAttachRegion (HandlerObj, ObjDesc, FALSE);
406     return (Status);
407 }
408 
409 
410 /*******************************************************************************
411  *
412  * FUNCTION:    AcpiEvInstallSpaceHandler
413  *
414  * PARAMETERS:  Node            - Namespace node for the device
415  *              SpaceId         - The address space ID
416  *              Handler         - Address of the handler
417  *              Setup           - Address of the setup function
418  *              Context         - Value passed to the handler on each access
419  *
420  * RETURN:      Status
421  *
422  * DESCRIPTION: Install a handler for all OpRegions of a given SpaceId.
423  *              Assumes namespace is locked
424  *
425  ******************************************************************************/
426 
427 ACPI_STATUS
428 AcpiEvInstallSpaceHandler (
429     ACPI_NAMESPACE_NODE     *Node,
430     ACPI_ADR_SPACE_TYPE     SpaceId,
431     ACPI_ADR_SPACE_HANDLER  Handler,
432     ACPI_ADR_SPACE_SETUP    Setup,
433     void                    *Context)
434 {
435     ACPI_OPERAND_OBJECT     *ObjDesc;
436     ACPI_OPERAND_OBJECT     *HandlerObj;
437     ACPI_STATUS             Status;
438     ACPI_OBJECT_TYPE        Type;
439     UINT8                  Flags = 0;
440 
441 
442     ACPI_FUNCTION_TRACE (EvInstallSpaceHandler);
443 
444 
445     /*
446      * This registration is valid for only the types below and the root. This
447      * is where the default handlers get placed.
448      */
449     if ((Node->Type != ACPI_TYPE_DEVICE)     &&
450         (Node->Type != ACPI_TYPE_PROCESSOR)  &&
451         (Node->Type != ACPI_TYPE_THERMAL)    &&
452         (Node != AcpiGbl_RootNode))
453     {
454         Status = AE_BAD_PARAMETER;
455         goto UnlockAndExit;
456     }
457 
458     if (Handler == ACPI_DEFAULT_HANDLER)
459     {
460         Flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
461 
462         switch (SpaceId)
463         {
464         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
465 
466             Handler = AcpiExSystemMemorySpaceHandler;
467             Setup   = AcpiEvSystemMemoryRegionSetup;
468             break;
469 
470         case ACPI_ADR_SPACE_SYSTEM_IO:
471 
472             Handler = AcpiExSystemIoSpaceHandler;
473             Setup   = AcpiEvIoSpaceRegionSetup;
474             break;
475 
476         case ACPI_ADR_SPACE_PCI_CONFIG:
477 
478             Handler = AcpiExPciConfigSpaceHandler;
479             Setup   = AcpiEvPciConfigRegionSetup;
480             break;
481 
482         case ACPI_ADR_SPACE_CMOS:
483 
484             Handler = AcpiExCmosSpaceHandler;
485             Setup   = AcpiEvCmosRegionSetup;
486             break;
487 
488         case ACPI_ADR_SPACE_PCI_BAR_TARGET:
489 
490             Handler = AcpiExPciBarSpaceHandler;
491             Setup   = AcpiEvPciBarRegionSetup;
492             break;
493 
494         case ACPI_ADR_SPACE_DATA_TABLE:
495 
496             Handler = AcpiExDataTableSpaceHandler;
497             Setup   = NULL;
498             break;
499 
500         default:
501 
502             Status = AE_BAD_PARAMETER;
503             goto UnlockAndExit;
504         }
505     }
506 
507     /* If the caller hasn't specified a setup routine, use the default */
508 
509     if (!Setup)
510     {
511         Setup = AcpiEvDefaultRegionSetup;
512     }
513 
514     /* Check for an existing internal object */
515 
516     ObjDesc = AcpiNsGetAttachedObject (Node);
517     if (ObjDesc)
518     {
519         /*
520          * The attached device object already exists. Make sure the handler
521          * is not already installed.
522          */
523         HandlerObj = ObjDesc->Device.Handler;
524 
525         /* Walk the handler list for this device */
526 
527         while (HandlerObj)
528         {
529             /* Same SpaceId indicates a handler already installed */
530 
531             if (HandlerObj->AddressSpace.SpaceId == SpaceId)
532             {
533                 if (HandlerObj->AddressSpace.Handler == Handler)
534                 {
535                     /*
536                      * It is (relatively) OK to attempt to install the SAME
537                      * handler twice. This can easily happen with the
538                      * PCI_Config space.
539                      */
540                     Status = AE_SAME_HANDLER;
541                     goto UnlockAndExit;
542                 }
543                 else
544                 {
545                     /* A handler is already installed */
546 
547                     Status = AE_ALREADY_EXISTS;
548                 }
549                 goto UnlockAndExit;
550             }
551 
552             /* Walk the linked list of handlers */
553 
554             HandlerObj = HandlerObj->AddressSpace.Next;
555         }
556     }
557     else
558     {
559         ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
560             "Creating object on Device %p while installing handler\n", Node));
561 
562         /* ObjDesc does not exist, create one */
563 
564         if (Node->Type == ACPI_TYPE_ANY)
565         {
566             Type = ACPI_TYPE_DEVICE;
567         }
568         else
569         {
570             Type = Node->Type;
571         }
572 
573         ObjDesc = AcpiUtCreateInternalObject (Type);
574         if (!ObjDesc)
575         {
576             Status = AE_NO_MEMORY;
577             goto UnlockAndExit;
578         }
579 
580         /* Init new descriptor */
581 
582         ObjDesc->Common.Type = (UINT8) Type;
583 
584         /* Attach the new object to the Node */
585 
586         Status = AcpiNsAttachObject (Node, ObjDesc, Type);
587 
588         /* Remove local reference to the object */
589 
590         AcpiUtRemoveReference (ObjDesc);
591 
592         if (ACPI_FAILURE (Status))
593         {
594             goto UnlockAndExit;
595         }
596     }
597 
598     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
599         "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
600         AcpiUtGetRegionName (SpaceId), SpaceId,
601         AcpiUtGetNodeName (Node), Node, ObjDesc));
602 
603     /*
604      * Install the handler
605      *
606      * At this point there is no existing handler. Just allocate the object
607      * for the handler and link it into the list.
608      */
609     HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
610     if (!HandlerObj)
611     {
612         Status = AE_NO_MEMORY;
613         goto UnlockAndExit;
614     }
615 
616     /* Init handler obj */
617 
618     HandlerObj->AddressSpace.SpaceId = (UINT8) SpaceId;
619     HandlerObj->AddressSpace.HandlerFlags = Flags;
620     HandlerObj->AddressSpace.RegionList = NULL;
621     HandlerObj->AddressSpace.Node = Node;
622     HandlerObj->AddressSpace.Handler = Handler;
623     HandlerObj->AddressSpace.Context = Context;
624     HandlerObj->AddressSpace.Setup  = Setup;
625 
626     /* Install at head of Device.AddressSpace list */
627 
628     HandlerObj->AddressSpace.Next = ObjDesc->Device.Handler;
629 
630     /*
631      * The Device object is the first reference on the HandlerObj.
632      * Each region that uses the handler adds a reference.
633      */
634     ObjDesc->Device.Handler = HandlerObj;
635 
636     /*
637      * Walk the namespace finding all of the regions this
638      * handler will manage.
639      *
640      * Start at the device and search the branch toward
641      * the leaf nodes until either the leaf is encountered or
642      * a device is detected that has an address handler of the
643      * same type.
644      *
645      * In either case, back up and search down the remainder
646      * of the branch
647      */
648     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
649                 ACPI_NS_WALK_UNLOCK, AcpiEvInstallHandler, NULL,
650                 HandlerObj, NULL);
651 
652 UnlockAndExit:
653     return_ACPI_STATUS (Status);
654 }
655