1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2/******************************************************************************
3 *
4 * Module Name: evevent - Fixed Event handling and dispatch
5 *
6 * Copyright (C) 2000 - 2020, Intel Corp.
7 *
8 *****************************************************************************/
9
10#include <acpi/acpi.h>
11#include "accommon.h"
12#include "acevents.h"
13
14#define _COMPONENT          ACPI_EVENTS
15ACPI_MODULE_NAME("evevent")
16#if (!ACPI_REDUCED_HARDWARE)	/* Entire module */
17/* Local prototypes */
18static acpi_status acpi_ev_fixed_event_initialize(void);
19
20static u32 acpi_ev_fixed_event_dispatch(u32 event);
21
22/*******************************************************************************
23 *
24 * FUNCTION:    acpi_ev_initialize_events
25 *
26 * PARAMETERS:  None
27 *
28 * RETURN:      Status
29 *
30 * DESCRIPTION: Initialize global data structures for ACPI events (Fixed, GPE)
31 *
32 ******************************************************************************/
33
34acpi_status acpi_ev_initialize_events(void)
35{
36	acpi_status status;
37
38	ACPI_FUNCTION_TRACE(ev_initialize_events);
39
40	/* If Hardware Reduced flag is set, there are no fixed events */
41
42	if (acpi_gbl_reduced_hardware) {
43		return_ACPI_STATUS(AE_OK);
44	}
45
46	/*
47	 * Initialize the Fixed and General Purpose Events. This is done prior to
48	 * enabling SCIs to prevent interrupts from occurring before the handlers
49	 * are installed.
50	 */
51	status = acpi_ev_fixed_event_initialize();
52	if (ACPI_FAILURE(status)) {
53		ACPI_EXCEPTION((AE_INFO, status,
54				"Unable to initialize fixed events"));
55		return_ACPI_STATUS(status);
56	}
57
58	status = acpi_ev_gpe_initialize();
59	if (ACPI_FAILURE(status)) {
60		ACPI_EXCEPTION((AE_INFO, status,
61				"Unable to initialize general purpose events"));
62		return_ACPI_STATUS(status);
63	}
64
65	return_ACPI_STATUS(status);
66}
67
68/*******************************************************************************
69 *
70 * FUNCTION:    acpi_ev_install_xrupt_handlers
71 *
72 * PARAMETERS:  None
73 *
74 * RETURN:      Status
75 *
76 * DESCRIPTION: Install interrupt handlers for the SCI and Global Lock
77 *
78 ******************************************************************************/
79
80acpi_status acpi_ev_install_xrupt_handlers(void)
81{
82	acpi_status status;
83
84	ACPI_FUNCTION_TRACE(ev_install_xrupt_handlers);
85
86	/* If Hardware Reduced flag is set, there is no ACPI h/w */
87
88	if (acpi_gbl_reduced_hardware) {
89		return_ACPI_STATUS(AE_OK);
90	}
91
92	/* Install the SCI handler */
93
94	status = acpi_ev_install_sci_handler();
95	if (ACPI_FAILURE(status)) {
96		ACPI_EXCEPTION((AE_INFO, status,
97				"Unable to install System Control Interrupt handler"));
98		return_ACPI_STATUS(status);
99	}
100
101	/* Install the handler for the Global Lock */
102
103	status = acpi_ev_init_global_lock_handler();
104	if (ACPI_FAILURE(status)) {
105		ACPI_EXCEPTION((AE_INFO, status,
106				"Unable to initialize Global Lock handler"));
107		return_ACPI_STATUS(status);
108	}
109
110	acpi_gbl_events_initialized = TRUE;
111	return_ACPI_STATUS(status);
112}
113
114/*******************************************************************************
115 *
116 * FUNCTION:    acpi_ev_fixed_event_initialize
117 *
118 * PARAMETERS:  None
119 *
120 * RETURN:      Status
121 *
122 * DESCRIPTION: Install the fixed event handlers and disable all fixed events.
123 *
124 ******************************************************************************/
125
126static acpi_status acpi_ev_fixed_event_initialize(void)
127{
128	u32 i;
129	acpi_status status;
130
131	/*
132	 * Initialize the structure that keeps track of fixed event handlers and
133	 * disable all of the fixed events.
134	 */
135	for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++) {
136		acpi_gbl_fixed_event_handlers[i].handler = NULL;
137		acpi_gbl_fixed_event_handlers[i].context = NULL;
138
139		/* Disable the fixed event */
140
141		if (acpi_gbl_fixed_event_info[i].enable_register_id != 0xFF) {
142			status =
143			    acpi_write_bit_register(acpi_gbl_fixed_event_info[i].enable_register_id,
144						    (i == ACPI_EVENT_PCIE_WAKE) ? ACPI_ENABLE_EVENT : ACPI_DISABLE_EVENT);
145			if (ACPI_FAILURE(status)) {
146				return (status);
147			}
148		}
149	}
150
151	return (AE_OK);
152}
153
154/*******************************************************************************
155 *
156 * FUNCTION:    acpi_ev_fixed_event_detect
157 *
158 * PARAMETERS:  None
159 *
160 * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
161 *
162 * DESCRIPTION: Checks the PM status register for active fixed events
163 *
164 ******************************************************************************/
165
166u32 acpi_ev_fixed_event_detect(void)
167{
168	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
169	u32 fixed_status;
170	u32 fixed_enable;
171	u32 i;
172	acpi_status status;
173
174	ACPI_FUNCTION_NAME(ev_fixed_event_detect);
175
176	/*
177	 * Read the fixed feature status and enable registers, as all the cases
178	 * depend on their values. Ignore errors here.
179	 */
180	status = acpi_hw_register_read(ACPI_REGISTER_PM1_STATUS, &fixed_status);
181	status |=
182	    acpi_hw_register_read(ACPI_REGISTER_PM1_ENABLE, &fixed_enable);
183	if (ACPI_FAILURE(status)) {
184		return (int_status);
185	}
186
187	if (fixed_enable & ACPI_BITMASK_PCIEXP_WAKE_DISABLE)
188		fixed_enable &= ~ACPI_BITMASK_PCIEXP_WAKE_DISABLE;
189	else
190		fixed_enable |= ACPI_BITMASK_PCIEXP_WAKE_DISABLE;
191
192	ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
193			  "Fixed Event Block: Enable %08X Status %08X\n",
194			  fixed_enable, fixed_status));
195
196	/*
197	 * Check for all possible Fixed Events and dispatch those that are active
198	 */
199	for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++) {
200
201		/* Both the status and enable bits must be on for this event */
202
203		if ((fixed_status & acpi_gbl_fixed_event_info[i].
204		     status_bit_mask)
205		    && (fixed_enable & acpi_gbl_fixed_event_info[i].
206			enable_bit_mask)) {
207			/*
208			 * Found an active (signalled) event. Invoke global event
209			 * handler if present.
210			 */
211			acpi_fixed_event_count[i]++;
212			if (acpi_gbl_global_event_handler) {
213				acpi_gbl_global_event_handler
214				    (ACPI_EVENT_TYPE_FIXED, NULL, i,
215				     acpi_gbl_global_event_handler_context);
216			}
217
218			int_status |= acpi_ev_fixed_event_dispatch(i);
219		}
220	}
221
222	return (int_status);
223}
224
225/*******************************************************************************
226 *
227 * FUNCTION:    acpi_ev_fixed_event_dispatch
228 *
229 * PARAMETERS:  event               - Event type
230 *
231 * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
232 *
233 * DESCRIPTION: Clears the status bit for the requested event, calls the
234 *              handler that previously registered for the event.
235 *              NOTE: If there is no handler for the event, the event is
236 *              disabled to prevent further interrupts.
237 *
238 ******************************************************************************/
239
240static u32 acpi_ev_fixed_event_dispatch(u32 event)
241{
242
243	ACPI_FUNCTION_ENTRY();
244
245	/* Clear the status bit */
246
247	(void)acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
248				      status_register_id, ACPI_CLEAR_STATUS);
249
250	/*
251	 * Make sure that a handler exists. If not, report an error
252	 * and disable the event to prevent further interrupts.
253	 */
254	if (!acpi_gbl_fixed_event_handlers[event].handler) {
255		(void)acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
256					      enable_register_id,
257					      event == ACPI_EVENT_PCIE_WAKE ? ACPI_ENABLE_EVENT : ACPI_DISABLE_EVENT);
258
259		ACPI_ERROR((AE_INFO,
260			    "No installed handler for fixed event - %s (%u), disabling",
261			    acpi_ut_get_event_name(event), event));
262
263		return (ACPI_INTERRUPT_NOT_HANDLED);
264	}
265
266	/* Invoke the Fixed Event handler */
267
268	return ((acpi_gbl_fixed_event_handlers[event].
269		 handler) (acpi_gbl_fixed_event_handlers[event].context));
270}
271
272/*******************************************************************************
273 *
274 * FUNCTION:    acpi_any_fixed_event_status_set
275 *
276 * PARAMETERS:  None
277 *
278 * RETURN:      TRUE or FALSE
279 *
280 * DESCRIPTION: Checks the PM status register for active fixed events
281 *
282 ******************************************************************************/
283
284u32 acpi_any_fixed_event_status_set(void)
285{
286	acpi_status status;
287	u32 in_status;
288	u32 in_enable;
289	u32 i;
290
291	status = acpi_hw_register_read(ACPI_REGISTER_PM1_ENABLE, &in_enable);
292	if (ACPI_FAILURE(status)) {
293		return (FALSE);
294	}
295
296	status = acpi_hw_register_read(ACPI_REGISTER_PM1_STATUS, &in_status);
297	if (ACPI_FAILURE(status)) {
298		return (FALSE);
299	}
300
301	/*
302	 * Check for all possible Fixed Events and dispatch those that are active
303	 */
304	for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++) {
305
306		/* Both the status and enable bits must be on for this event */
307
308		if ((in_status & acpi_gbl_fixed_event_info[i].status_bit_mask) &&
309		    (in_enable & acpi_gbl_fixed_event_info[i].enable_bit_mask)) {
310			return (TRUE);
311		}
312	}
313
314	return (FALSE);
315}
316
317#endif				/* !ACPI_REDUCED_HARDWARE */
318