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