xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpica/evgpe.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Module Name: evgpe - General Purpose Event handling and dispatch
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *****************************************************************************/
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "acevents.h"
13*4882a593Smuzhiyun #include "acnamesp.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define _COMPONENT          ACPI_EVENTS
16*4882a593Smuzhiyun ACPI_MODULE_NAME("evgpe")
17*4882a593Smuzhiyun #if (!ACPI_REDUCED_HARDWARE)	/* Entire module */
18*4882a593Smuzhiyun /* Local prototypes */
19*4882a593Smuzhiyun static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*******************************************************************************
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_update_gpe_enable_mask
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * PARAMETERS:  gpe_event_info          - GPE to update
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * RETURN:      Status
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * DESCRIPTION: Updates GPE register enable mask based upon whether there are
32*4882a593Smuzhiyun  *              runtime references to this GPE
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  ******************************************************************************/
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun acpi_status
acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info * gpe_event_info)37*4882a593Smuzhiyun acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	struct acpi_gpe_register_info *gpe_register_info;
40*4882a593Smuzhiyun 	u32 register_bit;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	gpe_register_info = gpe_event_info->register_info;
45*4882a593Smuzhiyun 	if (!gpe_register_info) {
46*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_NOT_EXIST);
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	/* Clear the run bit up front */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	/* Set the mask bit only if there are references to this GPE */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (gpe_event_info->runtime_count) {
58*4882a593Smuzhiyun 		ACPI_SET_BIT(gpe_register_info->enable_for_run,
59*4882a593Smuzhiyun 			     (u8)register_bit);
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	gpe_register_info->enable_mask = gpe_register_info->enable_for_run;
63*4882a593Smuzhiyun 	return_ACPI_STATUS(AE_OK);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /*******************************************************************************
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_enable_gpe
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * PARAMETERS:  gpe_event_info          - GPE to enable
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * RETURN:      Status
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * DESCRIPTION: Enable a GPE.
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  ******************************************************************************/
77*4882a593Smuzhiyun 
acpi_ev_enable_gpe(struct acpi_gpe_event_info * gpe_event_info)78*4882a593Smuzhiyun acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	acpi_status status;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_enable_gpe);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/* Enable the requested GPE */
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
87*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /*******************************************************************************
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_mask_gpe
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * PARAMETERS:  gpe_event_info          - GPE to be blocked/unblocked
95*4882a593Smuzhiyun  *              is_masked               - Whether the GPE is masked or not
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * RETURN:      Status
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * DESCRIPTION: Unconditionally mask/unmask a GPE during runtime.
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  ******************************************************************************/
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun acpi_status
acpi_ev_mask_gpe(struct acpi_gpe_event_info * gpe_event_info,u8 is_masked)104*4882a593Smuzhiyun acpi_ev_mask_gpe(struct acpi_gpe_event_info *gpe_event_info, u8 is_masked)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	struct acpi_gpe_register_info *gpe_register_info;
107*4882a593Smuzhiyun 	u32 register_bit;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_mask_gpe);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	gpe_register_info = gpe_event_info->register_info;
112*4882a593Smuzhiyun 	if (!gpe_register_info) {
113*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_NOT_EXIST);
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/* Perform the action */
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	if (is_masked) {
121*4882a593Smuzhiyun 		if (register_bit & gpe_register_info->mask_for_run) {
122*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_BAD_PARAMETER);
123*4882a593Smuzhiyun 		}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 		(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
126*4882a593Smuzhiyun 		ACPI_SET_BIT(gpe_register_info->mask_for_run, (u8)register_bit);
127*4882a593Smuzhiyun 	} else {
128*4882a593Smuzhiyun 		if (!(register_bit & gpe_register_info->mask_for_run)) {
129*4882a593Smuzhiyun 			return_ACPI_STATUS(AE_BAD_PARAMETER);
130*4882a593Smuzhiyun 		}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		ACPI_CLEAR_BIT(gpe_register_info->mask_for_run,
133*4882a593Smuzhiyun 			       (u8)register_bit);
134*4882a593Smuzhiyun 		if (gpe_event_info->runtime_count
135*4882a593Smuzhiyun 		    && !gpe_event_info->disable_for_dispatch) {
136*4882a593Smuzhiyun 			(void)acpi_hw_low_set_gpe(gpe_event_info,
137*4882a593Smuzhiyun 						  ACPI_GPE_ENABLE);
138*4882a593Smuzhiyun 		}
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	return_ACPI_STATUS(AE_OK);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /*******************************************************************************
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_add_gpe_reference
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * PARAMETERS:  gpe_event_info          - Add a reference to this GPE
149*4882a593Smuzhiyun  *              clear_on_enable         - Clear GPE status before enabling it
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  * RETURN:      Status
152*4882a593Smuzhiyun  *
153*4882a593Smuzhiyun  * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
154*4882a593Smuzhiyun  *              hardware-enabled.
155*4882a593Smuzhiyun  *
156*4882a593Smuzhiyun  ******************************************************************************/
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun acpi_status
acpi_ev_add_gpe_reference(struct acpi_gpe_event_info * gpe_event_info,u8 clear_on_enable)159*4882a593Smuzhiyun acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info,
160*4882a593Smuzhiyun 			  u8 clear_on_enable)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	acpi_status status = AE_OK;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
167*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_LIMIT);
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	gpe_event_info->runtime_count++;
171*4882a593Smuzhiyun 	if (gpe_event_info->runtime_count == 1) {
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		/* Enable on first reference */
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		if (clear_on_enable) {
176*4882a593Smuzhiyun 			(void)acpi_hw_clear_gpe(gpe_event_info);
177*4882a593Smuzhiyun 		}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 		status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
180*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status)) {
181*4882a593Smuzhiyun 			status = acpi_ev_enable_gpe(gpe_event_info);
182*4882a593Smuzhiyun 		}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
185*4882a593Smuzhiyun 			gpe_event_info->runtime_count--;
186*4882a593Smuzhiyun 		}
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /*******************************************************************************
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_remove_gpe_reference
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * PARAMETERS:  gpe_event_info          - Remove a reference to this GPE
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * RETURN:      Status
199*4882a593Smuzhiyun  *
200*4882a593Smuzhiyun  * DESCRIPTION: Remove a reference to a GPE. When the last reference is
201*4882a593Smuzhiyun  *              removed, the GPE is hardware-disabled.
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  ******************************************************************************/
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun acpi_status
acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info * gpe_event_info)206*4882a593Smuzhiyun acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	acpi_status status = AE_OK;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (!gpe_event_info->runtime_count) {
213*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_LIMIT);
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	gpe_event_info->runtime_count--;
217*4882a593Smuzhiyun 	if (!gpe_event_info->runtime_count) {
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 		/* Disable on last reference */
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
222*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status)) {
223*4882a593Smuzhiyun 			status =
224*4882a593Smuzhiyun 			    acpi_hw_low_set_gpe(gpe_event_info,
225*4882a593Smuzhiyun 						ACPI_GPE_DISABLE);
226*4882a593Smuzhiyun 		}
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
229*4882a593Smuzhiyun 			gpe_event_info->runtime_count++;
230*4882a593Smuzhiyun 		}
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /*******************************************************************************
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_low_get_gpe_info
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * PARAMETERS:  gpe_number          - Raw GPE number
241*4882a593Smuzhiyun  *              gpe_block           - A GPE info block
242*4882a593Smuzhiyun  *
243*4882a593Smuzhiyun  * RETURN:      A GPE event_info struct. NULL if not a valid GPE (The gpe_number
244*4882a593Smuzhiyun  *              is not within the specified GPE block)
245*4882a593Smuzhiyun  *
246*4882a593Smuzhiyun  * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
247*4882a593Smuzhiyun  *              the low-level implementation of ev_get_gpe_event_info.
248*4882a593Smuzhiyun  *
249*4882a593Smuzhiyun  ******************************************************************************/
250*4882a593Smuzhiyun 
acpi_ev_low_get_gpe_info(u32 gpe_number,struct acpi_gpe_block_info * gpe_block)251*4882a593Smuzhiyun struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
252*4882a593Smuzhiyun 						     struct acpi_gpe_block_info
253*4882a593Smuzhiyun 						     *gpe_block)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	u32 gpe_index;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/*
258*4882a593Smuzhiyun 	 * Validate that the gpe_number is within the specified gpe_block.
259*4882a593Smuzhiyun 	 * (Two steps)
260*4882a593Smuzhiyun 	 */
261*4882a593Smuzhiyun 	if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
262*4882a593Smuzhiyun 		return (NULL);
263*4882a593Smuzhiyun 	}
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	gpe_index = gpe_number - gpe_block->block_base_number;
266*4882a593Smuzhiyun 	if (gpe_index >= gpe_block->gpe_count) {
267*4882a593Smuzhiyun 		return (NULL);
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	return (&gpe_block->event_info[gpe_index]);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun /*******************************************************************************
275*4882a593Smuzhiyun  *
276*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_get_gpe_event_info
277*4882a593Smuzhiyun  *
278*4882a593Smuzhiyun  * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
279*4882a593Smuzhiyun  *              gpe_number          - Raw GPE number
280*4882a593Smuzhiyun  *
281*4882a593Smuzhiyun  * RETURN:      A GPE event_info struct. NULL if not a valid GPE
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * DESCRIPTION: Returns the event_info struct associated with this GPE.
284*4882a593Smuzhiyun  *              Validates the gpe_block and the gpe_number
285*4882a593Smuzhiyun  *
286*4882a593Smuzhiyun  *              Should be called only when the GPE lists are semaphore locked
287*4882a593Smuzhiyun  *              and not subject to change.
288*4882a593Smuzhiyun  *
289*4882a593Smuzhiyun  ******************************************************************************/
290*4882a593Smuzhiyun 
acpi_ev_get_gpe_event_info(acpi_handle gpe_device,u32 gpe_number)291*4882a593Smuzhiyun struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
292*4882a593Smuzhiyun 						       u32 gpe_number)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	union acpi_operand_object *obj_desc;
295*4882a593Smuzhiyun 	struct acpi_gpe_event_info *gpe_info;
296*4882a593Smuzhiyun 	u32 i;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	ACPI_FUNCTION_ENTRY();
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* A NULL gpe_device means use the FADT-defined GPE block(s) */
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	if (!gpe_device) {
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 		/* Examine GPE Block 0 and 1 (These blocks are permanent) */
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
307*4882a593Smuzhiyun 			gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
308*4882a593Smuzhiyun 							    acpi_gbl_gpe_fadt_blocks
309*4882a593Smuzhiyun 							    [i]);
310*4882a593Smuzhiyun 			if (gpe_info) {
311*4882a593Smuzhiyun 				return (gpe_info);
312*4882a593Smuzhiyun 			}
313*4882a593Smuzhiyun 		}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 		/* The gpe_number was not in the range of either FADT GPE block */
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 		return (NULL);
318*4882a593Smuzhiyun 	}
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* A Non-NULL gpe_device means this is a GPE Block Device */
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	obj_desc =
323*4882a593Smuzhiyun 	    acpi_ns_get_attached_object((struct acpi_namespace_node *)
324*4882a593Smuzhiyun 					       gpe_device);
325*4882a593Smuzhiyun 	if (!obj_desc || !obj_desc->device.gpe_block) {
326*4882a593Smuzhiyun 		return (NULL);
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	return (acpi_ev_low_get_gpe_info
330*4882a593Smuzhiyun 		(gpe_number, obj_desc->device.gpe_block));
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun /*******************************************************************************
334*4882a593Smuzhiyun  *
335*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_gpe_detect
336*4882a593Smuzhiyun  *
337*4882a593Smuzhiyun  * PARAMETERS:  gpe_xrupt_list      - Interrupt block for this interrupt.
338*4882a593Smuzhiyun  *                                    Can have multiple GPE blocks attached.
339*4882a593Smuzhiyun  *
340*4882a593Smuzhiyun  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
341*4882a593Smuzhiyun  *
342*4882a593Smuzhiyun  * DESCRIPTION: Detect if any GP events have occurred. This function is
343*4882a593Smuzhiyun  *              executed at interrupt level.
344*4882a593Smuzhiyun  *
345*4882a593Smuzhiyun  ******************************************************************************/
346*4882a593Smuzhiyun 
acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)347*4882a593Smuzhiyun u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun 	struct acpi_gpe_block_info *gpe_block;
350*4882a593Smuzhiyun 	struct acpi_namespace_node *gpe_device;
351*4882a593Smuzhiyun 	struct acpi_gpe_register_info *gpe_register_info;
352*4882a593Smuzhiyun 	struct acpi_gpe_event_info *gpe_event_info;
353*4882a593Smuzhiyun 	u32 gpe_number;
354*4882a593Smuzhiyun 	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
355*4882a593Smuzhiyun 	acpi_cpu_flags flags;
356*4882a593Smuzhiyun 	u32 i;
357*4882a593Smuzhiyun 	u32 j;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	ACPI_FUNCTION_NAME(ev_gpe_detect);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	/* Check for the case where there are no GPEs */
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	if (!gpe_xrupt_list) {
364*4882a593Smuzhiyun 		return (int_status);
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/*
368*4882a593Smuzhiyun 	 * We need to obtain the GPE lock for both the data structs and registers
369*4882a593Smuzhiyun 	 * Note: Not necessary to obtain the hardware lock, since the GPE
370*4882a593Smuzhiyun 	 * registers are owned by the gpe_lock.
371*4882a593Smuzhiyun 	 */
372*4882a593Smuzhiyun 	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	/* Examine all GPE blocks attached to this interrupt level */
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	gpe_block = gpe_xrupt_list->gpe_block_list_head;
377*4882a593Smuzhiyun 	while (gpe_block) {
378*4882a593Smuzhiyun 		gpe_device = gpe_block->node;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 		/*
381*4882a593Smuzhiyun 		 * Read all of the 8-bit GPE status and enable registers in this GPE
382*4882a593Smuzhiyun 		 * block, saving all of them. Find all currently active GP events.
383*4882a593Smuzhiyun 		 */
384*4882a593Smuzhiyun 		for (i = 0; i < gpe_block->register_count; i++) {
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 			/* Get the next status/enable pair */
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 			gpe_register_info = &gpe_block->register_info[i];
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 			/*
391*4882a593Smuzhiyun 			 * Optimization: If there are no GPEs enabled within this
392*4882a593Smuzhiyun 			 * register, we can safely ignore the entire register.
393*4882a593Smuzhiyun 			 */
394*4882a593Smuzhiyun 			if (!(gpe_register_info->enable_for_run |
395*4882a593Smuzhiyun 			      gpe_register_info->enable_for_wake)) {
396*4882a593Smuzhiyun 				ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
397*4882a593Smuzhiyun 						  "Ignore disabled registers for GPE %02X-%02X: "
398*4882a593Smuzhiyun 						  "RunEnable=%02X, WakeEnable=%02X\n",
399*4882a593Smuzhiyun 						  gpe_register_info->
400*4882a593Smuzhiyun 						  base_gpe_number,
401*4882a593Smuzhiyun 						  gpe_register_info->
402*4882a593Smuzhiyun 						  base_gpe_number +
403*4882a593Smuzhiyun 						  (ACPI_GPE_REGISTER_WIDTH - 1),
404*4882a593Smuzhiyun 						  gpe_register_info->
405*4882a593Smuzhiyun 						  enable_for_run,
406*4882a593Smuzhiyun 						  gpe_register_info->
407*4882a593Smuzhiyun 						  enable_for_wake));
408*4882a593Smuzhiyun 				continue;
409*4882a593Smuzhiyun 			}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 			/* Now look at the individual GPEs in this byte register */
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 			for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 				/* Detect and dispatch one GPE bit */
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 				gpe_event_info =
418*4882a593Smuzhiyun 				    &gpe_block->
419*4882a593Smuzhiyun 				    event_info[((acpi_size)i *
420*4882a593Smuzhiyun 						ACPI_GPE_REGISTER_WIDTH) + j];
421*4882a593Smuzhiyun 				gpe_number =
422*4882a593Smuzhiyun 				    j + gpe_register_info->base_gpe_number;
423*4882a593Smuzhiyun 				acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
424*4882a593Smuzhiyun 				int_status |=
425*4882a593Smuzhiyun 				    acpi_ev_detect_gpe(gpe_device,
426*4882a593Smuzhiyun 						       gpe_event_info,
427*4882a593Smuzhiyun 						       gpe_number);
428*4882a593Smuzhiyun 				flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
429*4882a593Smuzhiyun 			}
430*4882a593Smuzhiyun 		}
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 		gpe_block = gpe_block->next;
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
436*4882a593Smuzhiyun 	return (int_status);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun /*******************************************************************************
440*4882a593Smuzhiyun  *
441*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_asynch_execute_gpe_method
442*4882a593Smuzhiyun  *
443*4882a593Smuzhiyun  * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
444*4882a593Smuzhiyun  *
445*4882a593Smuzhiyun  * RETURN:      None
446*4882a593Smuzhiyun  *
447*4882a593Smuzhiyun  * DESCRIPTION: Perform the actual execution of a GPE control method. This
448*4882a593Smuzhiyun  *              function is called from an invocation of acpi_os_execute and
449*4882a593Smuzhiyun  *              therefore does NOT execute at interrupt level - so that
450*4882a593Smuzhiyun  *              the control method itself is not executed in the context of
451*4882a593Smuzhiyun  *              an interrupt handler.
452*4882a593Smuzhiyun  *
453*4882a593Smuzhiyun  ******************************************************************************/
454*4882a593Smuzhiyun 
acpi_ev_asynch_execute_gpe_method(void * context)455*4882a593Smuzhiyun static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	struct acpi_gpe_event_info *gpe_event_info = context;
458*4882a593Smuzhiyun 	acpi_status status = AE_OK;
459*4882a593Smuzhiyun 	struct acpi_evaluate_info *info;
460*4882a593Smuzhiyun 	struct acpi_gpe_notify_info *notify;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	/* Do the correct dispatch - normal method or implicit notify */
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
467*4882a593Smuzhiyun 	case ACPI_GPE_DISPATCH_NOTIFY:
468*4882a593Smuzhiyun 		/*
469*4882a593Smuzhiyun 		 * Implicit notify.
470*4882a593Smuzhiyun 		 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
471*4882a593Smuzhiyun 		 * NOTE: the request is queued for execution after this method
472*4882a593Smuzhiyun 		 * completes. The notify handlers are NOT invoked synchronously
473*4882a593Smuzhiyun 		 * from this thread -- because handlers may in turn run other
474*4882a593Smuzhiyun 		 * control methods.
475*4882a593Smuzhiyun 		 *
476*4882a593Smuzhiyun 		 * June 2012: Expand implicit notify mechanism to support
477*4882a593Smuzhiyun 		 * notifies on multiple device objects.
478*4882a593Smuzhiyun 		 */
479*4882a593Smuzhiyun 		notify = gpe_event_info->dispatch.notify_list;
480*4882a593Smuzhiyun 		while (ACPI_SUCCESS(status) && notify) {
481*4882a593Smuzhiyun 			status =
482*4882a593Smuzhiyun 			    acpi_ev_queue_notify_request(notify->device_node,
483*4882a593Smuzhiyun 							 ACPI_NOTIFY_DEVICE_WAKE);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 			notify = notify->next;
486*4882a593Smuzhiyun 		}
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 		break;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	case ACPI_GPE_DISPATCH_METHOD:
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		/* Allocate the evaluation information block */
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 		info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
495*4882a593Smuzhiyun 		if (!info) {
496*4882a593Smuzhiyun 			status = AE_NO_MEMORY;
497*4882a593Smuzhiyun 		} else {
498*4882a593Smuzhiyun 			/*
499*4882a593Smuzhiyun 			 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
500*4882a593Smuzhiyun 			 * _Lxx/_Exx control method that corresponds to this GPE
501*4882a593Smuzhiyun 			 */
502*4882a593Smuzhiyun 			info->prefix_node =
503*4882a593Smuzhiyun 			    gpe_event_info->dispatch.method_node;
504*4882a593Smuzhiyun 			info->flags = ACPI_IGNORE_RETURN_VALUE;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 			status = acpi_ns_evaluate(info);
507*4882a593Smuzhiyun 			ACPI_FREE(info);
508*4882a593Smuzhiyun 		}
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
511*4882a593Smuzhiyun 			ACPI_EXCEPTION((AE_INFO, status,
512*4882a593Smuzhiyun 					"while evaluating GPE method [%4.4s]",
513*4882a593Smuzhiyun 					acpi_ut_get_node_name(gpe_event_info->
514*4882a593Smuzhiyun 							      dispatch.
515*4882a593Smuzhiyun 							      method_node)));
516*4882a593Smuzhiyun 		}
517*4882a593Smuzhiyun 		break;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	default:
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 		goto error_exit;	/* Should never happen */
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	/* Defer enabling of GPE until all notify handlers are done */
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	status = acpi_os_execute(OSL_NOTIFY_HANDLER,
527*4882a593Smuzhiyun 				 acpi_ev_asynch_enable_gpe, gpe_event_info);
528*4882a593Smuzhiyun 	if (ACPI_SUCCESS(status)) {
529*4882a593Smuzhiyun 		return_VOID;
530*4882a593Smuzhiyun 	}
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun error_exit:
533*4882a593Smuzhiyun 	acpi_ev_asynch_enable_gpe(gpe_event_info);
534*4882a593Smuzhiyun 	return_VOID;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun /*******************************************************************************
539*4882a593Smuzhiyun  *
540*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_asynch_enable_gpe
541*4882a593Smuzhiyun  *
542*4882a593Smuzhiyun  * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
543*4882a593Smuzhiyun  *              Callback from acpi_os_execute
544*4882a593Smuzhiyun  *
545*4882a593Smuzhiyun  * RETURN:      None
546*4882a593Smuzhiyun  *
547*4882a593Smuzhiyun  * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
548*4882a593Smuzhiyun  *              complete (i.e., finish execution of Notify)
549*4882a593Smuzhiyun  *
550*4882a593Smuzhiyun  ******************************************************************************/
551*4882a593Smuzhiyun 
acpi_ev_asynch_enable_gpe(void * context)552*4882a593Smuzhiyun static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun 	struct acpi_gpe_event_info *gpe_event_info = context;
555*4882a593Smuzhiyun 	acpi_cpu_flags flags;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
558*4882a593Smuzhiyun 	(void)acpi_ev_finish_gpe(gpe_event_info);
559*4882a593Smuzhiyun 	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	return;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun /*******************************************************************************
566*4882a593Smuzhiyun  *
567*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_finish_gpe
568*4882a593Smuzhiyun  *
569*4882a593Smuzhiyun  * PARAMETERS:  gpe_event_info      - Info for this GPE
570*4882a593Smuzhiyun  *
571*4882a593Smuzhiyun  * RETURN:      Status
572*4882a593Smuzhiyun  *
573*4882a593Smuzhiyun  * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
574*4882a593Smuzhiyun  *              of a GPE method or a synchronous or asynchronous GPE handler.
575*4882a593Smuzhiyun  *
576*4882a593Smuzhiyun  ******************************************************************************/
577*4882a593Smuzhiyun 
acpi_ev_finish_gpe(struct acpi_gpe_event_info * gpe_event_info)578*4882a593Smuzhiyun acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun 	acpi_status status;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
583*4882a593Smuzhiyun 	    ACPI_GPE_LEVEL_TRIGGERED) {
584*4882a593Smuzhiyun 		/*
585*4882a593Smuzhiyun 		 * GPE is level-triggered, we clear the GPE status bit after
586*4882a593Smuzhiyun 		 * handling the event.
587*4882a593Smuzhiyun 		 */
588*4882a593Smuzhiyun 		status = acpi_hw_clear_gpe(gpe_event_info);
589*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
590*4882a593Smuzhiyun 			return (status);
591*4882a593Smuzhiyun 		}
592*4882a593Smuzhiyun 	}
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	/*
595*4882a593Smuzhiyun 	 * Enable this GPE, conditionally. This means that the GPE will
596*4882a593Smuzhiyun 	 * only be physically enabled if the enable_mask bit is set
597*4882a593Smuzhiyun 	 * in the event_info.
598*4882a593Smuzhiyun 	 */
599*4882a593Smuzhiyun 	(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
600*4882a593Smuzhiyun 	gpe_event_info->disable_for_dispatch = FALSE;
601*4882a593Smuzhiyun 	return (AE_OK);
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun /*******************************************************************************
606*4882a593Smuzhiyun  *
607*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_detect_gpe
608*4882a593Smuzhiyun  *
609*4882a593Smuzhiyun  * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
610*4882a593Smuzhiyun  *              gpe_event_info      - Info for this GPE
611*4882a593Smuzhiyun  *              gpe_number          - Number relative to the parent GPE block
612*4882a593Smuzhiyun  *
613*4882a593Smuzhiyun  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
614*4882a593Smuzhiyun  *
615*4882a593Smuzhiyun  * DESCRIPTION: Detect and dispatch a General Purpose Event to either a function
616*4882a593Smuzhiyun  *              (e.g. EC) or method (e.g. _Lxx/_Exx) handler.
617*4882a593Smuzhiyun  * NOTE:        GPE is W1C, so it is possible to handle a single GPE from both
618*4882a593Smuzhiyun  *              task and irq context in parallel as long as the process to
619*4882a593Smuzhiyun  *              detect and mask the GPE is atomic.
620*4882a593Smuzhiyun  *              However the atomicity of ACPI_GPE_DISPATCH_RAW_HANDLER is
621*4882a593Smuzhiyun  *              dependent on the raw handler itself.
622*4882a593Smuzhiyun  *
623*4882a593Smuzhiyun  ******************************************************************************/
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun u32
acpi_ev_detect_gpe(struct acpi_namespace_node * gpe_device,struct acpi_gpe_event_info * gpe_event_info,u32 gpe_number)626*4882a593Smuzhiyun acpi_ev_detect_gpe(struct acpi_namespace_node *gpe_device,
627*4882a593Smuzhiyun 		   struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
630*4882a593Smuzhiyun 	u8 enabled_status_byte;
631*4882a593Smuzhiyun 	u64 status_reg;
632*4882a593Smuzhiyun 	u64 enable_reg;
633*4882a593Smuzhiyun 	u32 register_bit;
634*4882a593Smuzhiyun 	struct acpi_gpe_register_info *gpe_register_info;
635*4882a593Smuzhiyun 	struct acpi_gpe_handler_info *gpe_handler_info;
636*4882a593Smuzhiyun 	acpi_cpu_flags flags;
637*4882a593Smuzhiyun 	acpi_status status;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_gpe_detect);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	if (!gpe_event_info) {
644*4882a593Smuzhiyun 		gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
645*4882a593Smuzhiyun 		if (!gpe_event_info)
646*4882a593Smuzhiyun 			goto error_exit;
647*4882a593Smuzhiyun 	}
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	/* Get the info block for the entire GPE register */
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	gpe_register_info = gpe_event_info->register_info;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	/* Get the register bitmask for this GPE */
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	/* GPE currently enabled (enable bit == 1)? */
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	status = acpi_hw_gpe_read(&enable_reg, &gpe_register_info->enable_address);
660*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
661*4882a593Smuzhiyun 		goto error_exit;
662*4882a593Smuzhiyun 	}
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	/* GPE currently active (status bit == 1)? */
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	status = acpi_hw_gpe_read(&status_reg, &gpe_register_info->status_address);
667*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
668*4882a593Smuzhiyun 		goto error_exit;
669*4882a593Smuzhiyun 	}
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	/* Check if there is anything active at all in this GPE */
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
674*4882a593Smuzhiyun 			  "Read registers for GPE %02X: Status=%02X, Enable=%02X, "
675*4882a593Smuzhiyun 			  "RunEnable=%02X, WakeEnable=%02X\n",
676*4882a593Smuzhiyun 			  gpe_number,
677*4882a593Smuzhiyun 			  (u32)(status_reg & register_bit),
678*4882a593Smuzhiyun 			  (u32)(enable_reg & register_bit),
679*4882a593Smuzhiyun 			  gpe_register_info->enable_for_run,
680*4882a593Smuzhiyun 			  gpe_register_info->enable_for_wake));
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	enabled_status_byte = (u8)(status_reg & enable_reg);
683*4882a593Smuzhiyun 	if (!(enabled_status_byte & register_bit)) {
684*4882a593Smuzhiyun 		goto error_exit;
685*4882a593Smuzhiyun 	}
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	/* Invoke global event handler if present */
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	acpi_gpe_count++;
690*4882a593Smuzhiyun 	if (acpi_gbl_global_event_handler) {
691*4882a593Smuzhiyun 		acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE,
692*4882a593Smuzhiyun 					      gpe_device, gpe_number,
693*4882a593Smuzhiyun 					      acpi_gbl_global_event_handler_context);
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	/* Found an active GPE */
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
699*4882a593Smuzhiyun 	    ACPI_GPE_DISPATCH_RAW_HANDLER) {
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 		/* Dispatch the event to a raw handler */
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 		gpe_handler_info = gpe_event_info->dispatch.handler;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 		/*
706*4882a593Smuzhiyun 		 * There is no protection around the namespace node
707*4882a593Smuzhiyun 		 * and the GPE handler to ensure a safe destruction
708*4882a593Smuzhiyun 		 * because:
709*4882a593Smuzhiyun 		 * 1. The namespace node is expected to always
710*4882a593Smuzhiyun 		 *    exist after loading a table.
711*4882a593Smuzhiyun 		 * 2. The GPE handler is expected to be flushed by
712*4882a593Smuzhiyun 		 *    acpi_os_wait_events_complete() before the
713*4882a593Smuzhiyun 		 *    destruction.
714*4882a593Smuzhiyun 		 */
715*4882a593Smuzhiyun 		acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
716*4882a593Smuzhiyun 		int_status |=
717*4882a593Smuzhiyun 		    gpe_handler_info->address(gpe_device, gpe_number,
718*4882a593Smuzhiyun 					      gpe_handler_info->context);
719*4882a593Smuzhiyun 		flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
720*4882a593Smuzhiyun 	} else {
721*4882a593Smuzhiyun 		/* Dispatch the event to a standard handler or method. */
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 		int_status |= acpi_ev_gpe_dispatch(gpe_device,
724*4882a593Smuzhiyun 						   gpe_event_info, gpe_number);
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun error_exit:
728*4882a593Smuzhiyun 	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
729*4882a593Smuzhiyun 	return (int_status);
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun /*******************************************************************************
733*4882a593Smuzhiyun  *
734*4882a593Smuzhiyun  * FUNCTION:    acpi_ev_gpe_dispatch
735*4882a593Smuzhiyun  *
736*4882a593Smuzhiyun  * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
737*4882a593Smuzhiyun  *              gpe_event_info      - Info for this GPE
738*4882a593Smuzhiyun  *              gpe_number          - Number relative to the parent GPE block
739*4882a593Smuzhiyun  *
740*4882a593Smuzhiyun  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
741*4882a593Smuzhiyun  *
742*4882a593Smuzhiyun  * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
743*4882a593Smuzhiyun  *              or method (e.g. _Lxx/_Exx) handler.
744*4882a593Smuzhiyun  *
745*4882a593Smuzhiyun  ******************************************************************************/
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun u32
acpi_ev_gpe_dispatch(struct acpi_namespace_node * gpe_device,struct acpi_gpe_event_info * gpe_event_info,u32 gpe_number)748*4882a593Smuzhiyun acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
749*4882a593Smuzhiyun 		     struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	acpi_status status;
752*4882a593Smuzhiyun 	u32 return_value;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	/*
757*4882a593Smuzhiyun 	 * Always disable the GPE so that it does not keep firing before
758*4882a593Smuzhiyun 	 * any asynchronous activity completes (either from the execution
759*4882a593Smuzhiyun 	 * of a GPE method or an asynchronous GPE handler.)
760*4882a593Smuzhiyun 	 *
761*4882a593Smuzhiyun 	 * If there is no handler or method to run, just disable the
762*4882a593Smuzhiyun 	 * GPE and leave it disabled permanently to prevent further such
763*4882a593Smuzhiyun 	 * pointless events from firing.
764*4882a593Smuzhiyun 	 */
765*4882a593Smuzhiyun 	status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
766*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
767*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status,
768*4882a593Smuzhiyun 				"Unable to disable GPE %02X", gpe_number));
769*4882a593Smuzhiyun 		return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
770*4882a593Smuzhiyun 	}
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	/*
773*4882a593Smuzhiyun 	 * If edge-triggered, clear the GPE status bit now. Note that
774*4882a593Smuzhiyun 	 * level-triggered events are cleared after the GPE is serviced.
775*4882a593Smuzhiyun 	 */
776*4882a593Smuzhiyun 	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
777*4882a593Smuzhiyun 	    ACPI_GPE_EDGE_TRIGGERED) {
778*4882a593Smuzhiyun 		status = acpi_hw_clear_gpe(gpe_event_info);
779*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
780*4882a593Smuzhiyun 			ACPI_EXCEPTION((AE_INFO, status,
781*4882a593Smuzhiyun 					"Unable to clear GPE %02X",
782*4882a593Smuzhiyun 					gpe_number));
783*4882a593Smuzhiyun 			(void)acpi_hw_low_set_gpe(gpe_event_info,
784*4882a593Smuzhiyun 						  ACPI_GPE_CONDITIONAL_ENABLE);
785*4882a593Smuzhiyun 			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
786*4882a593Smuzhiyun 		}
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	gpe_event_info->disable_for_dispatch = TRUE;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	/*
792*4882a593Smuzhiyun 	 * Dispatch the GPE to either an installed handler or the control
793*4882a593Smuzhiyun 	 * method associated with this GPE (_Lxx or _Exx). If a handler
794*4882a593Smuzhiyun 	 * exists, we invoke it and do not attempt to run the method.
795*4882a593Smuzhiyun 	 * If there is neither a handler nor a method, leave the GPE
796*4882a593Smuzhiyun 	 * disabled.
797*4882a593Smuzhiyun 	 */
798*4882a593Smuzhiyun 	switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
799*4882a593Smuzhiyun 	case ACPI_GPE_DISPATCH_HANDLER:
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 		/* Invoke the installed handler (at interrupt level) */
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 		return_value =
804*4882a593Smuzhiyun 		    gpe_event_info->dispatch.handler->address(gpe_device,
805*4882a593Smuzhiyun 							      gpe_number,
806*4882a593Smuzhiyun 							      gpe_event_info->
807*4882a593Smuzhiyun 							      dispatch.handler->
808*4882a593Smuzhiyun 							      context);
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 		/* If requested, clear (if level-triggered) and re-enable the GPE */
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 		if (return_value & ACPI_REENABLE_GPE) {
813*4882a593Smuzhiyun 			(void)acpi_ev_finish_gpe(gpe_event_info);
814*4882a593Smuzhiyun 		}
815*4882a593Smuzhiyun 		break;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	case ACPI_GPE_DISPATCH_METHOD:
818*4882a593Smuzhiyun 	case ACPI_GPE_DISPATCH_NOTIFY:
819*4882a593Smuzhiyun 		/*
820*4882a593Smuzhiyun 		 * Execute the method associated with the GPE
821*4882a593Smuzhiyun 		 * NOTE: Level-triggered GPEs are cleared after the method completes.
822*4882a593Smuzhiyun 		 */
823*4882a593Smuzhiyun 		status = acpi_os_execute(OSL_GPE_HANDLER,
824*4882a593Smuzhiyun 					 acpi_ev_asynch_execute_gpe_method,
825*4882a593Smuzhiyun 					 gpe_event_info);
826*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
827*4882a593Smuzhiyun 			ACPI_EXCEPTION((AE_INFO, status,
828*4882a593Smuzhiyun 					"Unable to queue handler for GPE %02X - event disabled",
829*4882a593Smuzhiyun 					gpe_number));
830*4882a593Smuzhiyun 		}
831*4882a593Smuzhiyun 		break;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	default:
834*4882a593Smuzhiyun 		/*
835*4882a593Smuzhiyun 		 * No handler or method to run!
836*4882a593Smuzhiyun 		 * 03/2010: This case should no longer be possible. We will not allow
837*4882a593Smuzhiyun 		 * a GPE to be enabled if it has no handler or method.
838*4882a593Smuzhiyun 		 */
839*4882a593Smuzhiyun 		ACPI_ERROR((AE_INFO,
840*4882a593Smuzhiyun 			    "No handler or method for GPE %02X, disabling event",
841*4882a593Smuzhiyun 			    gpe_number));
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 		break;
844*4882a593Smuzhiyun 	}
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	return_UINT32(ACPI_INTERRUPT_HANDLED);
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun #endif				/* !ACPI_REDUCED_HARDWARE */
850