Sync ACPICA with Intel's version 20180508 (from previously 20170831).
[dragonfly.git] / sys / contrib / dev / acpica / source / components / events / evhandler.c
blobc4dca59a3fed4ba7885b51d90cfcc89ba30aca98
1 /******************************************************************************
3 * Module Name: evhandler - Support for Address Space handlers
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
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.
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
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;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
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.
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.
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.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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.
80 * 4. Disclaimer and Export Compliance
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.
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.
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.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
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.
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.
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.
150 *****************************************************************************/
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acevents.h"
155 #include "acnamesp.h"
156 #include "acinterp.h"
158 #define _COMPONENT ACPI_EVENTS
159 ACPI_MODULE_NAME ("evhandler")
162 /* Local prototypes */
164 static ACPI_STATUS
165 AcpiEvInstallHandler (
166 ACPI_HANDLE ObjHandle,
167 UINT32 Level,
168 void *Context,
169 void **ReturnValue);
172 /* These are the address spaces that will get default handlers */
174 UINT8 AcpiGbl_DefaultAddressSpaces[ACPI_NUM_DEFAULT_SPACES] =
176 ACPI_ADR_SPACE_SYSTEM_MEMORY,
177 ACPI_ADR_SPACE_SYSTEM_IO,
178 ACPI_ADR_SPACE_PCI_CONFIG,
179 ACPI_ADR_SPACE_DATA_TABLE
183 /*******************************************************************************
185 * FUNCTION: AcpiEvInstallRegionHandlers
187 * PARAMETERS: None
189 * RETURN: Status
191 * DESCRIPTION: Installs the core subsystem default address space handlers.
193 ******************************************************************************/
195 ACPI_STATUS
196 AcpiEvInstallRegionHandlers (
197 void)
199 ACPI_STATUS Status;
200 UINT32 i;
203 ACPI_FUNCTION_TRACE (EvInstallRegionHandlers);
206 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
207 if (ACPI_FAILURE (Status))
209 return_ACPI_STATUS (Status);
213 * All address spaces (PCI Config, EC, SMBus) are scope dependent and
214 * registration must occur for a specific device.
216 * In the case of the system memory and IO address spaces there is
217 * currently no device associated with the address space. For these we
218 * use the root.
220 * We install the default PCI config space handler at the root so that
221 * this space is immediately available even though the we have not
222 * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
223 * specification which states that the PCI config space must be always
224 * available -- even though we are nowhere near ready to find the PCI root
225 * buses at this point.
227 * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
228 * has already been installed (via AcpiInstallAddressSpaceHandler).
229 * Similar for AE_SAME_HANDLER.
231 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
233 Status = AcpiEvInstallSpaceHandler (AcpiGbl_RootNode,
234 AcpiGbl_DefaultAddressSpaces[i],
235 ACPI_DEFAULT_HANDLER, NULL, NULL);
236 switch (Status)
238 case AE_OK:
239 case AE_SAME_HANDLER:
240 case AE_ALREADY_EXISTS:
242 /* These exceptions are all OK */
244 Status = AE_OK;
245 break;
247 default:
249 goto UnlockAndExit;
253 UnlockAndExit:
254 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
255 return_ACPI_STATUS (Status);
259 /*******************************************************************************
261 * FUNCTION: AcpiEvHasDefaultHandler
263 * PARAMETERS: Node - Namespace node for the device
264 * SpaceId - The address space ID
266 * RETURN: TRUE if default handler is installed, FALSE otherwise
268 * DESCRIPTION: Check if the default handler is installed for the requested
269 * space ID.
271 ******************************************************************************/
273 BOOLEAN
274 AcpiEvHasDefaultHandler (
275 ACPI_NAMESPACE_NODE *Node,
276 ACPI_ADR_SPACE_TYPE SpaceId)
278 ACPI_OPERAND_OBJECT *ObjDesc;
279 ACPI_OPERAND_OBJECT *HandlerObj;
282 /* Must have an existing internal object */
284 ObjDesc = AcpiNsGetAttachedObject (Node);
285 if (ObjDesc)
287 HandlerObj = ObjDesc->CommonNotify.Handler;
289 /* Walk the linked list of handlers for this object */
291 while (HandlerObj)
293 if (HandlerObj->AddressSpace.SpaceId == SpaceId)
295 if (HandlerObj->AddressSpace.HandlerFlags &
296 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
298 return (TRUE);
302 HandlerObj = HandlerObj->AddressSpace.Next;
306 return (FALSE);
310 /*******************************************************************************
312 * FUNCTION: AcpiEvInstallHandler
314 * PARAMETERS: WalkNamespace callback
316 * DESCRIPTION: This routine installs an address handler into objects that are
317 * of type Region or Device.
319 * If the Object is a Device, and the device has a handler of
320 * the same type then the search is terminated in that branch.
322 * This is because the existing handler is closer in proximity
323 * to any more regions than the one we are trying to install.
325 ******************************************************************************/
327 static ACPI_STATUS
328 AcpiEvInstallHandler (
329 ACPI_HANDLE ObjHandle,
330 UINT32 Level,
331 void *Context,
332 void **ReturnValue)
334 ACPI_OPERAND_OBJECT *HandlerObj;
335 ACPI_OPERAND_OBJECT *NextHandlerObj;
336 ACPI_OPERAND_OBJECT *ObjDesc;
337 ACPI_NAMESPACE_NODE *Node;
338 ACPI_STATUS Status;
341 ACPI_FUNCTION_NAME (EvInstallHandler);
344 HandlerObj = (ACPI_OPERAND_OBJECT *) Context;
346 /* Parameter validation */
348 if (!HandlerObj)
350 return (AE_OK);
353 /* Convert and validate the device handle */
355 Node = AcpiNsValidateHandle (ObjHandle);
356 if (!Node)
358 return (AE_BAD_PARAMETER);
362 * We only care about regions and objects that are allowed to have
363 * address space handlers
365 if ((Node->Type != ACPI_TYPE_DEVICE) &&
366 (Node->Type != ACPI_TYPE_REGION) &&
367 (Node != AcpiGbl_RootNode))
369 return (AE_OK);
372 /* Check for an existing internal object */
374 ObjDesc = AcpiNsGetAttachedObject (Node);
375 if (!ObjDesc)
377 /* No object, just exit */
379 return (AE_OK);
382 /* Devices are handled different than regions */
384 if (ObjDesc->Common.Type == ACPI_TYPE_DEVICE)
386 /* Check if this Device already has a handler for this address space */
388 NextHandlerObj = AcpiEvFindRegionHandler (
389 HandlerObj->AddressSpace.SpaceId, ObjDesc->CommonNotify.Handler);
390 if (NextHandlerObj)
392 /* Found a handler, is it for the same address space? */
394 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
395 "Found handler for region [%s] in device %p(%p) handler %p\n",
396 AcpiUtGetRegionName (HandlerObj->AddressSpace.SpaceId),
397 ObjDesc, NextHandlerObj, HandlerObj));
400 * Since the object we found it on was a device, then it means
401 * that someone has already installed a handler for the branch
402 * of the namespace from this device on. Just bail out telling
403 * the walk routine to not traverse this branch. This preserves
404 * the scoping rule for handlers.
406 return (AE_CTRL_DEPTH);
410 * As long as the device didn't have a handler for this space we
411 * don't care about it. We just ignore it and proceed.
413 return (AE_OK);
416 /* Object is a Region */
418 if (ObjDesc->Region.SpaceId != HandlerObj->AddressSpace.SpaceId)
420 /* This region is for a different address space, just ignore it */
422 return (AE_OK);
426 * Now we have a region and it is for the handler's address space type.
428 * First disconnect region for any previous handler (if any)
430 AcpiEvDetachRegion (ObjDesc, FALSE);
432 /* Connect the region to the new handler */
434 Status = AcpiEvAttachRegion (HandlerObj, ObjDesc, FALSE);
435 return (Status);
439 /*******************************************************************************
441 * FUNCTION: AcpiEvFindRegionHandler
443 * PARAMETERS: SpaceId - The address space ID
444 * HandlerObj - Head of the handler object list
446 * RETURN: Matching handler object. NULL if space ID not matched
448 * DESCRIPTION: Search a handler object list for a match on the address
449 * space ID.
451 ******************************************************************************/
453 ACPI_OPERAND_OBJECT *
454 AcpiEvFindRegionHandler (
455 ACPI_ADR_SPACE_TYPE SpaceId,
456 ACPI_OPERAND_OBJECT *HandlerObj)
459 /* Walk the handler list for this device */
461 while (HandlerObj)
463 /* Same SpaceId indicates a handler is installed */
465 if (HandlerObj->AddressSpace.SpaceId == SpaceId)
467 return (HandlerObj);
470 /* Next handler object */
472 HandlerObj = HandlerObj->AddressSpace.Next;
475 return (NULL);
479 /*******************************************************************************
481 * FUNCTION: AcpiEvInstallSpaceHandler
483 * PARAMETERS: Node - Namespace node for the device
484 * SpaceId - The address space ID
485 * Handler - Address of the handler
486 * Setup - Address of the setup function
487 * Context - Value passed to the handler on each access
489 * RETURN: Status
491 * DESCRIPTION: Install a handler for all OpRegions of a given SpaceId.
492 * Assumes namespace is locked
494 ******************************************************************************/
496 ACPI_STATUS
497 AcpiEvInstallSpaceHandler (
498 ACPI_NAMESPACE_NODE *Node,
499 ACPI_ADR_SPACE_TYPE SpaceId,
500 ACPI_ADR_SPACE_HANDLER Handler,
501 ACPI_ADR_SPACE_SETUP Setup,
502 void *Context)
504 ACPI_OPERAND_OBJECT *ObjDesc;
505 ACPI_OPERAND_OBJECT *HandlerObj;
506 ACPI_STATUS Status = AE_OK;
507 ACPI_OBJECT_TYPE Type;
508 UINT8 Flags = 0;
511 ACPI_FUNCTION_TRACE (EvInstallSpaceHandler);
515 * This registration is valid for only the types below and the root.
516 * The root node is where the default handlers get installed.
518 if ((Node->Type != ACPI_TYPE_DEVICE) &&
519 (Node->Type != ACPI_TYPE_PROCESSOR) &&
520 (Node->Type != ACPI_TYPE_THERMAL) &&
521 (Node != AcpiGbl_RootNode))
523 Status = AE_BAD_PARAMETER;
524 goto UnlockAndExit;
527 if (Handler == ACPI_DEFAULT_HANDLER)
529 Flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
531 switch (SpaceId)
533 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
535 Handler = AcpiExSystemMemorySpaceHandler;
536 Setup = AcpiEvSystemMemoryRegionSetup;
537 break;
539 case ACPI_ADR_SPACE_SYSTEM_IO:
541 Handler = AcpiExSystemIoSpaceHandler;
542 Setup = AcpiEvIoSpaceRegionSetup;
543 break;
545 case ACPI_ADR_SPACE_PCI_CONFIG:
547 Handler = AcpiExPciConfigSpaceHandler;
548 Setup = AcpiEvPciConfigRegionSetup;
549 break;
551 case ACPI_ADR_SPACE_CMOS:
553 Handler = AcpiExCmosSpaceHandler;
554 Setup = AcpiEvCmosRegionSetup;
555 break;
557 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
559 Handler = AcpiExPciBarSpaceHandler;
560 Setup = AcpiEvPciBarRegionSetup;
561 break;
563 case ACPI_ADR_SPACE_DATA_TABLE:
565 Handler = AcpiExDataTableSpaceHandler;
566 Setup = NULL;
567 break;
569 default:
571 Status = AE_BAD_PARAMETER;
572 goto UnlockAndExit;
576 /* If the caller hasn't specified a setup routine, use the default */
578 if (!Setup)
580 Setup = AcpiEvDefaultRegionSetup;
583 /* Check for an existing internal object */
585 ObjDesc = AcpiNsGetAttachedObject (Node);
586 if (ObjDesc)
589 * The attached device object already exists. Now make sure
590 * the handler is not already installed.
592 HandlerObj = AcpiEvFindRegionHandler (SpaceId,
593 ObjDesc->CommonNotify.Handler);
595 if (HandlerObj)
597 if (HandlerObj->AddressSpace.Handler == Handler)
600 * It is (relatively) OK to attempt to install the SAME
601 * handler twice. This can easily happen with the
602 * PCI_Config space.
604 Status = AE_SAME_HANDLER;
605 goto UnlockAndExit;
607 else
609 /* A handler is already installed */
611 Status = AE_ALREADY_EXISTS;
614 goto UnlockAndExit;
617 else
619 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
620 "Creating object on Device %p while installing handler\n",
621 Node));
623 /* ObjDesc does not exist, create one */
625 if (Node->Type == ACPI_TYPE_ANY)
627 Type = ACPI_TYPE_DEVICE;
629 else
631 Type = Node->Type;
634 ObjDesc = AcpiUtCreateInternalObject (Type);
635 if (!ObjDesc)
637 Status = AE_NO_MEMORY;
638 goto UnlockAndExit;
641 /* Init new descriptor */
643 ObjDesc->Common.Type = (UINT8) Type;
645 /* Attach the new object to the Node */
647 Status = AcpiNsAttachObject (Node, ObjDesc, Type);
649 /* Remove local reference to the object */
651 AcpiUtRemoveReference (ObjDesc);
653 if (ACPI_FAILURE (Status))
655 goto UnlockAndExit;
659 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
660 "Installing address handler for region %s(%X) "
661 "on Device %4.4s %p(%p)\n",
662 AcpiUtGetRegionName (SpaceId), SpaceId,
663 AcpiUtGetNodeName (Node), Node, ObjDesc));
666 * Install the handler
668 * At this point there is no existing handler. Just allocate the object
669 * for the handler and link it into the list.
671 HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
672 if (!HandlerObj)
674 Status = AE_NO_MEMORY;
675 goto UnlockAndExit;
678 /* Init handler obj */
680 HandlerObj->AddressSpace.SpaceId = (UINT8) SpaceId;
681 HandlerObj->AddressSpace.HandlerFlags = Flags;
682 HandlerObj->AddressSpace.RegionList = NULL;
683 HandlerObj->AddressSpace.Node = Node;
684 HandlerObj->AddressSpace.Handler = Handler;
685 HandlerObj->AddressSpace.Context = Context;
686 HandlerObj->AddressSpace.Setup = Setup;
688 /* Install at head of Device.AddressSpace list */
690 HandlerObj->AddressSpace.Next = ObjDesc->CommonNotify.Handler;
693 * The Device object is the first reference on the HandlerObj.
694 * Each region that uses the handler adds a reference.
696 ObjDesc->CommonNotify.Handler = HandlerObj;
699 * Walk the namespace finding all of the regions this handler will
700 * manage.
702 * Start at the device and search the branch toward the leaf nodes
703 * until either the leaf is encountered or a device is detected that
704 * has an address handler of the same type.
706 * In either case, back up and search down the remainder of the branch
708 Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node,
709 ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
710 AcpiEvInstallHandler, NULL, HandlerObj, NULL);
712 UnlockAndExit:
713 return_ACPI_STATUS (Status);