xref: /reactos/drivers/bus/acpi/acpica/events/evevent.c (revision f0dd1e7b)
1c2c66affSColin Finck /******************************************************************************
2c2c66affSColin Finck  *
3c2c66affSColin Finck  * Module Name: evevent - Fixed Event handling and dispatch
4c2c66affSColin Finck  *
5c2c66affSColin Finck  *****************************************************************************/
6c2c66affSColin Finck 
7c2c66affSColin Finck /*
803b24380SThomas Faber  * Copyright (C) 2000 - 2022, Intel Corp.
9c2c66affSColin Finck  * All rights reserved.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
12c2c66affSColin Finck  * modification, are permitted provided that the following conditions
13c2c66affSColin Finck  * are met:
14c2c66affSColin Finck  * 1. Redistributions of source code must retain the above copyright
15c2c66affSColin Finck  *    notice, this list of conditions, and the following disclaimer,
16c2c66affSColin Finck  *    without modification.
17c2c66affSColin Finck  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18c2c66affSColin Finck  *    substantially similar to the "NO WARRANTY" disclaimer below
19c2c66affSColin Finck  *    ("Disclaimer") and any redistribution must be conditioned upon
20c2c66affSColin Finck  *    including a substantially similar Disclaimer requirement for further
21c2c66affSColin Finck  *    binary redistribution.
22c2c66affSColin Finck  * 3. Neither the names of the above-listed copyright holders nor the names
23c2c66affSColin Finck  *    of any contributors may be used to endorse or promote products derived
24c2c66affSColin Finck  *    from this software without specific prior written permission.
25c2c66affSColin Finck  *
26c2c66affSColin Finck  * Alternatively, this software may be distributed under the terms of the
27c2c66affSColin Finck  * GNU General Public License ("GPL") version 2 as published by the Free
28c2c66affSColin Finck  * Software Foundation.
29c2c66affSColin Finck  *
30c2c66affSColin Finck  * NO WARRANTY
31c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32c2c66affSColin Finck  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
336eb8cc49SThomas Faber  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34c2c66affSColin Finck  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35c2c66affSColin Finck  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36c2c66affSColin Finck  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37c2c66affSColin Finck  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38c2c66affSColin Finck  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39c2c66affSColin Finck  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40c2c66affSColin Finck  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGES.
42c2c66affSColin Finck  */
43c2c66affSColin Finck 
44c2c66affSColin Finck #include "acpi.h"
45c2c66affSColin Finck #include "accommon.h"
46c2c66affSColin Finck #include "acevents.h"
47c2c66affSColin Finck 
48c2c66affSColin Finck #define _COMPONENT          ACPI_EVENTS
49c2c66affSColin Finck         ACPI_MODULE_NAME    ("evevent")
50c2c66affSColin Finck 
51c2c66affSColin Finck #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
52c2c66affSColin Finck 
53c2c66affSColin Finck /* Local prototypes */
54c2c66affSColin Finck 
55c2c66affSColin Finck static ACPI_STATUS
56c2c66affSColin Finck AcpiEvFixedEventInitialize (
57c2c66affSColin Finck     void);
58c2c66affSColin Finck 
59c2c66affSColin Finck static UINT32
60c2c66affSColin Finck AcpiEvFixedEventDispatch (
61c2c66affSColin Finck     UINT32                  Event);
62c2c66affSColin Finck 
63c2c66affSColin Finck 
64c2c66affSColin Finck /*******************************************************************************
65c2c66affSColin Finck  *
66c2c66affSColin Finck  * FUNCTION:    AcpiEvInitializeEvents
67c2c66affSColin Finck  *
68c2c66affSColin Finck  * PARAMETERS:  None
69c2c66affSColin Finck  *
70c2c66affSColin Finck  * RETURN:      Status
71c2c66affSColin Finck  *
72c2c66affSColin Finck  * DESCRIPTION: Initialize global data structures for ACPI events (Fixed, GPE)
73c2c66affSColin Finck  *
74c2c66affSColin Finck  ******************************************************************************/
75c2c66affSColin Finck 
76c2c66affSColin Finck ACPI_STATUS
AcpiEvInitializeEvents(void)77c2c66affSColin Finck AcpiEvInitializeEvents (
78c2c66affSColin Finck     void)
79c2c66affSColin Finck {
80c2c66affSColin Finck     ACPI_STATUS             Status;
81c2c66affSColin Finck 
82c2c66affSColin Finck 
83c2c66affSColin Finck     ACPI_FUNCTION_TRACE (EvInitializeEvents);
84c2c66affSColin Finck 
85c2c66affSColin Finck 
86c2c66affSColin Finck     /* If Hardware Reduced flag is set, there are no fixed events */
87c2c66affSColin Finck 
88c2c66affSColin Finck     if (AcpiGbl_ReducedHardware)
89c2c66affSColin Finck     {
90c2c66affSColin Finck         return_ACPI_STATUS (AE_OK);
91c2c66affSColin Finck     }
92c2c66affSColin Finck 
93c2c66affSColin Finck     /*
94c2c66affSColin Finck      * Initialize the Fixed and General Purpose Events. This is done prior to
95c2c66affSColin Finck      * enabling SCIs to prevent interrupts from occurring before the handlers
96c2c66affSColin Finck      * are installed.
97c2c66affSColin Finck      */
98c2c66affSColin Finck     Status = AcpiEvFixedEventInitialize ();
99c2c66affSColin Finck     if (ACPI_FAILURE (Status))
100c2c66affSColin Finck     {
101c2c66affSColin Finck         ACPI_EXCEPTION ((AE_INFO, Status,
102c2c66affSColin Finck             "Unable to initialize fixed events"));
103c2c66affSColin Finck         return_ACPI_STATUS (Status);
104c2c66affSColin Finck     }
105c2c66affSColin Finck 
106c2c66affSColin Finck     Status = AcpiEvGpeInitialize ();
107c2c66affSColin Finck     if (ACPI_FAILURE (Status))
108c2c66affSColin Finck     {
109c2c66affSColin Finck         ACPI_EXCEPTION ((AE_INFO, Status,
110c2c66affSColin Finck             "Unable to initialize general purpose events"));
111c2c66affSColin Finck         return_ACPI_STATUS (Status);
112c2c66affSColin Finck     }
113c2c66affSColin Finck 
114c2c66affSColin Finck     return_ACPI_STATUS (Status);
115c2c66affSColin Finck }
116c2c66affSColin Finck 
117c2c66affSColin Finck 
118c2c66affSColin Finck /*******************************************************************************
119c2c66affSColin Finck  *
120c2c66affSColin Finck  * FUNCTION:    AcpiEvInstallXruptHandlers
121c2c66affSColin Finck  *
122c2c66affSColin Finck  * PARAMETERS:  None
123c2c66affSColin Finck  *
124c2c66affSColin Finck  * RETURN:      Status
125c2c66affSColin Finck  *
126c2c66affSColin Finck  * DESCRIPTION: Install interrupt handlers for the SCI and Global Lock
127c2c66affSColin Finck  *
128c2c66affSColin Finck  ******************************************************************************/
129c2c66affSColin Finck 
130c2c66affSColin Finck ACPI_STATUS
AcpiEvInstallXruptHandlers(void)131c2c66affSColin Finck AcpiEvInstallXruptHandlers (
132c2c66affSColin Finck     void)
133c2c66affSColin Finck {
134c2c66affSColin Finck     ACPI_STATUS             Status;
135c2c66affSColin Finck 
136c2c66affSColin Finck 
137c2c66affSColin Finck     ACPI_FUNCTION_TRACE (EvInstallXruptHandlers);
138c2c66affSColin Finck 
139c2c66affSColin Finck 
140c2c66affSColin Finck     /* If Hardware Reduced flag is set, there is no ACPI h/w */
141c2c66affSColin Finck 
142c2c66affSColin Finck     if (AcpiGbl_ReducedHardware)
143c2c66affSColin Finck     {
144c2c66affSColin Finck         return_ACPI_STATUS (AE_OK);
145c2c66affSColin Finck     }
146c2c66affSColin Finck 
147c2c66affSColin Finck     /* Install the SCI handler */
148c2c66affSColin Finck 
149c2c66affSColin Finck     Status = AcpiEvInstallSciHandler ();
150c2c66affSColin Finck     if (ACPI_FAILURE (Status))
151c2c66affSColin Finck     {
152c2c66affSColin Finck         ACPI_EXCEPTION ((AE_INFO, Status,
153c2c66affSColin Finck             "Unable to install System Control Interrupt handler"));
154c2c66affSColin Finck         return_ACPI_STATUS (Status);
155c2c66affSColin Finck     }
156c2c66affSColin Finck 
157c2c66affSColin Finck     /* Install the handler for the Global Lock */
158c2c66affSColin Finck 
159c2c66affSColin Finck     Status = AcpiEvInitGlobalLockHandler ();
160c2c66affSColin Finck     if (ACPI_FAILURE (Status))
161c2c66affSColin Finck     {
162c2c66affSColin Finck         ACPI_EXCEPTION ((AE_INFO, Status,
163c2c66affSColin Finck             "Unable to initialize Global Lock handler"));
164c2c66affSColin Finck         return_ACPI_STATUS (Status);
165c2c66affSColin Finck     }
166c2c66affSColin Finck 
167c2c66affSColin Finck     AcpiGbl_EventsInitialized = TRUE;
168c2c66affSColin Finck     return_ACPI_STATUS (Status);
169c2c66affSColin Finck }
170c2c66affSColin Finck 
171c2c66affSColin Finck 
172c2c66affSColin Finck /*******************************************************************************
173c2c66affSColin Finck  *
174c2c66affSColin Finck  * FUNCTION:    AcpiEvFixedEventInitialize
175c2c66affSColin Finck  *
176c2c66affSColin Finck  * PARAMETERS:  None
177c2c66affSColin Finck  *
178c2c66affSColin Finck  * RETURN:      Status
179c2c66affSColin Finck  *
180c2c66affSColin Finck  * DESCRIPTION: Install the fixed event handlers and disable all fixed events.
181c2c66affSColin Finck  *
182c2c66affSColin Finck  ******************************************************************************/
183c2c66affSColin Finck 
184c2c66affSColin Finck static ACPI_STATUS
AcpiEvFixedEventInitialize(void)185c2c66affSColin Finck AcpiEvFixedEventInitialize (
186c2c66affSColin Finck     void)
187c2c66affSColin Finck {
188c2c66affSColin Finck     UINT32                  i;
189c2c66affSColin Finck     ACPI_STATUS             Status;
190c2c66affSColin Finck 
191c2c66affSColin Finck 
192c2c66affSColin Finck     /*
193c2c66affSColin Finck      * Initialize the structure that keeps track of fixed event handlers and
194696cdc63SThomas Faber      * disable all of the fixed events.
195c2c66affSColin Finck      */
196c2c66affSColin Finck     for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++)
197c2c66affSColin Finck     {
198c2c66affSColin Finck         AcpiGbl_FixedEventHandlers[i].Handler = NULL;
199c2c66affSColin Finck         AcpiGbl_FixedEventHandlers[i].Context = NULL;
200c2c66affSColin Finck 
201c2c66affSColin Finck         /* Disable the fixed event */
202c2c66affSColin Finck 
203c2c66affSColin Finck         if (AcpiGbl_FixedEventInfo[i].EnableRegisterId != 0xFF)
204c2c66affSColin Finck         {
205c2c66affSColin Finck             Status = AcpiWriteBitRegister (
206c2c66affSColin Finck                 AcpiGbl_FixedEventInfo[i].EnableRegisterId,
207*f0dd1e7bSThomas Faber                 (i == ACPI_EVENT_PCIE_WAKE) ?
208*f0dd1e7bSThomas Faber                 ACPI_ENABLE_EVENT : ACPI_DISABLE_EVENT);
209c2c66affSColin Finck             if (ACPI_FAILURE (Status))
210c2c66affSColin Finck             {
211c2c66affSColin Finck                 return (Status);
212c2c66affSColin Finck             }
213c2c66affSColin Finck         }
214c2c66affSColin Finck     }
215c2c66affSColin Finck 
216c2c66affSColin Finck     return (AE_OK);
217c2c66affSColin Finck }
218c2c66affSColin Finck 
219c2c66affSColin Finck 
220c2c66affSColin Finck /*******************************************************************************
221c2c66affSColin Finck  *
222c2c66affSColin Finck  * FUNCTION:    AcpiEvFixedEventDetect
223c2c66affSColin Finck  *
224c2c66affSColin Finck  * PARAMETERS:  None
225c2c66affSColin Finck  *
226c2c66affSColin Finck  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
227c2c66affSColin Finck  *
228c2c66affSColin Finck  * DESCRIPTION: Checks the PM status register for active fixed events
229c2c66affSColin Finck  *
230c2c66affSColin Finck  ******************************************************************************/
231c2c66affSColin Finck 
232c2c66affSColin Finck UINT32
AcpiEvFixedEventDetect(void)233c2c66affSColin Finck AcpiEvFixedEventDetect (
234c2c66affSColin Finck     void)
235c2c66affSColin Finck {
236c2c66affSColin Finck     UINT32                  IntStatus = ACPI_INTERRUPT_NOT_HANDLED;
237c2c66affSColin Finck     UINT32                  FixedStatus;
238c2c66affSColin Finck     UINT32                  FixedEnable;
239c2c66affSColin Finck     UINT32                  i;
2406847cc3aSThomas Faber     ACPI_STATUS             Status;
241c2c66affSColin Finck 
242c2c66affSColin Finck 
243c2c66affSColin Finck     ACPI_FUNCTION_NAME (EvFixedEventDetect);
244c2c66affSColin Finck 
245c2c66affSColin Finck 
246c2c66affSColin Finck     /*
247c2c66affSColin Finck      * Read the fixed feature status and enable registers, as all the cases
248c2c66affSColin Finck      * depend on their values. Ignore errors here.
249c2c66affSColin Finck      */
2506847cc3aSThomas Faber     Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_STATUS, &FixedStatus);
2516847cc3aSThomas Faber     Status |= AcpiHwRegisterRead (ACPI_REGISTER_PM1_ENABLE, &FixedEnable);
2526847cc3aSThomas Faber     if (ACPI_FAILURE (Status))
2536847cc3aSThomas Faber     {
2546847cc3aSThomas Faber         return (IntStatus);
2556847cc3aSThomas Faber     }
256c2c66affSColin Finck 
257*f0dd1e7bSThomas Faber     if (FixedEnable & ACPI_BITMASK_PCIEXP_WAKE_DISABLE)
258*f0dd1e7bSThomas Faber          FixedEnable &= ~ACPI_BITMASK_PCIEXP_WAKE_DISABLE;
259*f0dd1e7bSThomas Faber     else
260*f0dd1e7bSThomas Faber          FixedEnable |= ACPI_BITMASK_PCIEXP_WAKE_DISABLE;
261*f0dd1e7bSThomas Faber 
262c2c66affSColin Finck     ACPI_DEBUG_PRINT ((ACPI_DB_INTERRUPTS,
263c2c66affSColin Finck         "Fixed Event Block: Enable %08X Status %08X\n",
264c2c66affSColin Finck         FixedEnable, FixedStatus));
265c2c66affSColin Finck 
266c2c66affSColin Finck     /*
267c2c66affSColin Finck      * Check for all possible Fixed Events and dispatch those that are active
268c2c66affSColin Finck      */
269c2c66affSColin Finck     for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++)
270c2c66affSColin Finck     {
271c2c66affSColin Finck         /* Both the status and enable bits must be on for this event */
272c2c66affSColin Finck 
273c2c66affSColin Finck         if ((FixedStatus & AcpiGbl_FixedEventInfo[i].StatusBitMask) &&
274c2c66affSColin Finck             (FixedEnable & AcpiGbl_FixedEventInfo[i].EnableBitMask))
275c2c66affSColin Finck         {
276c2c66affSColin Finck             /*
277c2c66affSColin Finck              * Found an active (signalled) event. Invoke global event
278c2c66affSColin Finck              * handler if present.
279c2c66affSColin Finck              */
280c2c66affSColin Finck             AcpiFixedEventCount[i]++;
281c2c66affSColin Finck             if (AcpiGbl_GlobalEventHandler)
282c2c66affSColin Finck             {
283c2c66affSColin Finck                 AcpiGbl_GlobalEventHandler (ACPI_EVENT_TYPE_FIXED, NULL,
284c2c66affSColin Finck                      i, AcpiGbl_GlobalEventHandlerContext);
285c2c66affSColin Finck             }
286c2c66affSColin Finck 
287c2c66affSColin Finck             IntStatus |= AcpiEvFixedEventDispatch (i);
288c2c66affSColin Finck         }
289c2c66affSColin Finck     }
290c2c66affSColin Finck 
291c2c66affSColin Finck     return (IntStatus);
292c2c66affSColin Finck }
293c2c66affSColin Finck 
294c2c66affSColin Finck 
295c2c66affSColin Finck /*******************************************************************************
296c2c66affSColin Finck  *
297c2c66affSColin Finck  * FUNCTION:    AcpiEvFixedEventDispatch
298c2c66affSColin Finck  *
299c2c66affSColin Finck  * PARAMETERS:  Event               - Event type
300c2c66affSColin Finck  *
301c2c66affSColin Finck  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
302c2c66affSColin Finck  *
303c2c66affSColin Finck  * DESCRIPTION: Clears the status bit for the requested event, calls the
304c2c66affSColin Finck  *              handler that previously registered for the event.
305c2c66affSColin Finck  *              NOTE: If there is no handler for the event, the event is
306c2c66affSColin Finck  *              disabled to prevent further interrupts.
307c2c66affSColin Finck  *
308c2c66affSColin Finck  ******************************************************************************/
309c2c66affSColin Finck 
310c2c66affSColin Finck static UINT32
AcpiEvFixedEventDispatch(UINT32 Event)311c2c66affSColin Finck AcpiEvFixedEventDispatch (
312c2c66affSColin Finck     UINT32                  Event)
313c2c66affSColin Finck {
314c2c66affSColin Finck 
315c2c66affSColin Finck     ACPI_FUNCTION_ENTRY ();
316c2c66affSColin Finck 
317c2c66affSColin Finck 
318c2c66affSColin Finck     /* Clear the status bit */
319c2c66affSColin Finck 
320c2c66affSColin Finck     (void) AcpiWriteBitRegister (
321c2c66affSColin Finck         AcpiGbl_FixedEventInfo[Event].StatusRegisterId,
322c2c66affSColin Finck         ACPI_CLEAR_STATUS);
323c2c66affSColin Finck 
324c2c66affSColin Finck     /*
325c2c66affSColin Finck      * Make sure that a handler exists. If not, report an error
326c2c66affSColin Finck      * and disable the event to prevent further interrupts.
327c2c66affSColin Finck      */
328c2c66affSColin Finck     if (!AcpiGbl_FixedEventHandlers[Event].Handler)
329c2c66affSColin Finck     {
330c2c66affSColin Finck         (void) AcpiWriteBitRegister (
331c2c66affSColin Finck             AcpiGbl_FixedEventInfo[Event].EnableRegisterId,
332*f0dd1e7bSThomas Faber             (Event == ACPI_EVENT_PCIE_WAKE) ?
333*f0dd1e7bSThomas Faber 	    ACPI_ENABLE_EVENT : ACPI_DISABLE_EVENT);
334c2c66affSColin Finck 
335c2c66affSColin Finck         ACPI_ERROR ((AE_INFO,
336c2c66affSColin Finck             "No installed handler for fixed event - %s (%u), disabling",
337c2c66affSColin Finck             AcpiUtGetEventName (Event), Event));
338c2c66affSColin Finck 
339c2c66affSColin Finck         return (ACPI_INTERRUPT_NOT_HANDLED);
340c2c66affSColin Finck     }
341c2c66affSColin Finck 
342c2c66affSColin Finck     /* Invoke the Fixed Event handler */
343c2c66affSColin Finck 
344c2c66affSColin Finck     return ((AcpiGbl_FixedEventHandlers[Event].Handler)(
345c2c66affSColin Finck         AcpiGbl_FixedEventHandlers[Event].Context));
346c2c66affSColin Finck }
347c2c66affSColin Finck 
348c2c66affSColin Finck #endif /* !ACPI_REDUCED_HARDWARE */
349