blob: c5a06882bdf636a5f6e9f8dd51aa1c7dbc555ce3 [file] [log] [blame]
Erik Schmauss95857632018-03-14 16:13:07 -07001// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/******************************************************************************
3 *
4 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *
Bob Moore4441e552021-01-15 10:48:25 -08006 * Copyright (C) 2000 - 2021, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Erik Schmauss95857632018-03-14 16:13:07 -07008 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050011#include "accommon.h"
12#include "acevents.h"
13#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#define _COMPONENT ACPI_EVENTS
Len Brown4be44fc2005-08-05 00:44:28 -040016ACPI_MODULE_NAME("evgpe")
Bob Moore33620c52012-02-14 18:14:27 +080017#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
Robert Moore44f6c012005-04-18 22:49:35 -040018/* Local prototypes */
Len Brown4be44fc2005-08-05 00:44:28 -040019static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Lin Ming5a284cd2010-12-13 13:39:07 +080021static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*******************************************************************************
24 *
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080025 * FUNCTION: acpi_ev_update_gpe_enable_mask
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 * PARAMETERS: gpe_event_info - GPE to update
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
29 * RETURN: Status
30 *
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080031 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
32 * runtime references to this GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 ******************************************************************************/
35
36acpi_status
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080037acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Len Brown4be44fc2005-08-05 00:44:28 -040039 struct acpi_gpe_register_info *gpe_register_info;
Rafael J. Wysockie4e9a732010-06-08 10:48:26 +020040 u32 register_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080042 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 gpe_register_info = gpe_event_info->register_info;
45 if (!gpe_register_info) {
Len Brown4be44fc2005-08-05 00:44:28 -040046 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
Bob Moored4913dc2009-03-06 10:05:18 +080048
Feng Tang1d94e1e2012-08-17 11:10:02 +080049 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080051 /* Clear the run bit up front */
Lin Ming0f849d22010-04-06 14:52:37 +080052
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +010053 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080055 /* Set the mask bit only if there are references to this GPE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Lin Ming0f849d22010-04-06 14:52:37 +080057 if (gpe_event_info->runtime_count) {
Lv Zheng1f86e8c2012-10-31 02:25:45 +000058 ACPI_SET_BIT(gpe_register_info->enable_for_run,
59 (u8)register_bit);
Lin Ming0f849d22010-04-06 14:52:37 +080060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Bob Moore1fad8732015-12-29 13:54:36 +080062 gpe_register_info->enable_mask = gpe_register_info->enable_for_run;
Len Brown4be44fc2005-08-05 00:44:28 -040063 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080066/*******************************************************************************
67 *
68 * FUNCTION: acpi_ev_enable_gpe
69 *
Lv Zhengf19f1a72014-10-10 10:39:39 +080070 * PARAMETERS: gpe_event_info - GPE to enable
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080071 *
72 * RETURN: Status
73 *
Erik Schmauss18996f22018-03-14 16:12:57 -070074 * DESCRIPTION: Enable a GPE.
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080075 *
76 ******************************************************************************/
Lv Zhengf19f1a72014-10-10 10:39:39 +080077
Lv Zheng1f86e8c2012-10-31 02:25:45 +000078acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080079{
80 acpi_status status;
81
82 ACPI_FUNCTION_TRACE(ev_enable_gpe);
83
Furquan Shaikhc8b19172019-03-20 15:28:44 -070084 /* Enable the requested GPE */
Rafael J. Wysocki2c2a2fb2019-04-30 11:18:21 +020085
Rafael J. Wysocki0ee0d342015-04-15 04:00:27 +020086 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080087 return_ACPI_STATUS(status);
88}
89
Lin Ming0f849d22010-04-06 14:52:37 +080090/*******************************************************************************
91 *
Lv Zheng2af52c22016-08-04 16:43:39 +080092 * FUNCTION: acpi_ev_mask_gpe
93 *
94 * PARAMETERS: gpe_event_info - GPE to be blocked/unblocked
95 * is_masked - Whether the GPE is masked or not
96 *
97 * RETURN: Status
98 *
99 * DESCRIPTION: Unconditionally mask/unmask a GPE during runtime.
100 *
101 ******************************************************************************/
102
103acpi_status
104acpi_ev_mask_gpe(struct acpi_gpe_event_info *gpe_event_info, u8 is_masked)
105{
106 struct acpi_gpe_register_info *gpe_register_info;
107 u32 register_bit;
108
109 ACPI_FUNCTION_TRACE(ev_mask_gpe);
110
111 gpe_register_info = gpe_event_info->register_info;
112 if (!gpe_register_info) {
113 return_ACPI_STATUS(AE_NOT_EXIST);
114 }
115
116 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
117
118 /* Perform the action */
119
120 if (is_masked) {
121 if (register_bit & gpe_register_info->mask_for_run) {
122 return_ACPI_STATUS(AE_BAD_PARAMETER);
123 }
124
125 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
126 ACPI_SET_BIT(gpe_register_info->mask_for_run, (u8)register_bit);
127 } else {
128 if (!(register_bit & gpe_register_info->mask_for_run)) {
129 return_ACPI_STATUS(AE_BAD_PARAMETER);
130 }
131
132 ACPI_CLEAR_BIT(gpe_register_info->mask_for_run,
133 (u8)register_bit);
134 if (gpe_event_info->runtime_count
135 && !gpe_event_info->disable_for_dispatch) {
136 (void)acpi_hw_low_set_gpe(gpe_event_info,
137 ACPI_GPE_ENABLE);
138 }
139 }
140
141 return_ACPI_STATUS(AE_OK);
142}
143
144/*******************************************************************************
145 *
Lin Ming3a378982010-12-13 13:36:15 +0800146 * FUNCTION: acpi_ev_add_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200147 *
Lin Mingda503372010-12-13 13:39:37 +0800148 * PARAMETERS: gpe_event_info - Add a reference to this GPE
Rafael J. Wysocki44758ba2019-06-17 13:31:45 +0200149 * clear_on_enable - Clear GPE status before enabling it
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200150 *
151 * RETURN: Status
152 *
153 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
154 * hardware-enabled.
155 *
156 ******************************************************************************/
157
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000158acpi_status
Rafael J. Wysocki44758ba2019-06-17 13:31:45 +0200159acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info,
160 u8 clear_on_enable)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200161{
162 acpi_status status = AE_OK;
163
Lin Ming3a378982010-12-13 13:36:15 +0800164 ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
165
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200166 if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
167 return_ACPI_STATUS(AE_LIMIT);
168 }
169
170 gpe_event_info->runtime_count++;
171 if (gpe_event_info->runtime_count == 1) {
Lin Mingda503372010-12-13 13:39:37 +0800172
173 /* Enable on first reference */
174
Rafael J. Wysocki44758ba2019-06-17 13:31:45 +0200175 if (clear_on_enable) {
176 (void)acpi_hw_clear_gpe(gpe_event_info);
177 }
178
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200179 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
180 if (ACPI_SUCCESS(status)) {
181 status = acpi_ev_enable_gpe(gpe_event_info);
182 }
183
184 if (ACPI_FAILURE(status)) {
185 gpe_event_info->runtime_count--;
186 }
187 }
188
189 return_ACPI_STATUS(status);
190}
191
192/*******************************************************************************
193 *
Lin Ming3a378982010-12-13 13:36:15 +0800194 * FUNCTION: acpi_ev_remove_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200195 *
Lin Mingda503372010-12-13 13:39:37 +0800196 * PARAMETERS: gpe_event_info - Remove a reference to this GPE
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200197 *
198 * RETURN: Status
199 *
200 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
201 * removed, the GPE is hardware-disabled.
202 *
203 ******************************************************************************/
204
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000205acpi_status
206acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200207{
208 acpi_status status = AE_OK;
209
Lin Ming3a378982010-12-13 13:36:15 +0800210 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
211
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200212 if (!gpe_event_info->runtime_count) {
213 return_ACPI_STATUS(AE_LIMIT);
214 }
215
216 gpe_event_info->runtime_count--;
217 if (!gpe_event_info->runtime_count) {
Lin Mingda503372010-12-13 13:39:37 +0800218
219 /* Disable on last reference */
220
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200221 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
222 if (ACPI_SUCCESS(status)) {
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000223 status =
224 acpi_hw_low_set_gpe(gpe_event_info,
Rafael J. Wysocki0ee0d342015-04-15 04:00:27 +0200225 ACPI_GPE_DISABLE);
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200226 }
227
228 if (ACPI_FAILURE(status)) {
229 gpe_event_info->runtime_count++;
230 }
231 }
232
233 return_ACPI_STATUS(status);
234}
235
236/*******************************************************************************
237 *
Lin Ming0f849d22010-04-06 14:52:37 +0800238 * FUNCTION: acpi_ev_low_get_gpe_info
239 *
240 * PARAMETERS: gpe_number - Raw GPE number
241 * gpe_block - A GPE info block
242 *
243 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
244 * is not within the specified GPE block)
245 *
246 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
247 * the low-level implementation of ev_get_gpe_event_info.
248 *
249 ******************************************************************************/
250
251struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
252 struct acpi_gpe_block_info
253 *gpe_block)
254{
255 u32 gpe_index;
256
257 /*
258 * Validate that the gpe_number is within the specified gpe_block.
259 * (Two steps)
260 */
261 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
262 return (NULL);
263 }
264
265 gpe_index = gpe_number - gpe_block->block_base_number;
266 if (gpe_index >= gpe_block->gpe_count) {
267 return (NULL);
268 }
269
270 return (&gpe_block->event_info[gpe_index]);
271}
272
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*******************************************************************************
275 *
276 * FUNCTION: acpi_ev_get_gpe_event_info
277 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800278 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * gpe_number - Raw GPE number
280 *
281 * RETURN: A GPE event_info struct. NULL if not a valid GPE
282 *
283 * DESCRIPTION: Returns the event_info struct associated with this GPE.
284 * Validates the gpe_block and the gpe_number
285 *
286 * Should be called only when the GPE lists are semaphore locked
287 * and not subject to change.
288 *
289 ******************************************************************************/
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
292 u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Len Brown4be44fc2005-08-05 00:44:28 -0400294 union acpi_operand_object *obj_desc;
Lin Ming0f849d22010-04-06 14:52:37 +0800295 struct acpi_gpe_event_info *gpe_info;
Bob Moore67a119f2008-06-10 13:42:13 +0800296 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Len Brown4be44fc2005-08-05 00:44:28 -0400298 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Bob Moore186c3072010-04-27 11:32:28 +0800300 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (!gpe_device) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
305
306 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
Lin Ming0f849d22010-04-06 14:52:37 +0800307 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
308 acpi_gbl_gpe_fadt_blocks
309 [i]);
310 if (gpe_info) {
311 return (gpe_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
314
315 /* The gpe_number was not in the range of either FADT GPE block */
316
317 return (NULL);
318 }
319
320 /* A Non-NULL gpe_device means this is a GPE Block Device */
321
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000322 obj_desc =
323 acpi_ns_get_attached_object((struct acpi_namespace_node *)
Len Brownfd350942007-05-09 23:34:35 -0400324 gpe_device);
Len Brown4be44fc2005-08-05 00:44:28 -0400325 if (!obj_desc || !obj_desc->device.gpe_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return (NULL);
327 }
328
Lin Ming0f849d22010-04-06 14:52:37 +0800329 return (acpi_ev_low_get_gpe_info
330 (gpe_number, obj_desc->device.gpe_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/*******************************************************************************
334 *
335 * FUNCTION: acpi_ev_gpe_detect
336 *
337 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
338 * Can have multiple GPE blocks attached.
339 *
340 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
341 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800342 * DESCRIPTION: Detect if any GP events have occurred. This function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * executed at interrupt level.
344 *
345 ******************************************************************************/
346
Lv Zhengf19f1a72014-10-10 10:39:39 +0800347u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Len Brown4be44fc2005-08-05 00:44:28 -0400349 struct acpi_gpe_block_info *gpe_block;
Lv Zheng779ba5a2015-02-05 15:20:22 +0800350 struct acpi_namespace_node *gpe_device;
Bob Moore08978312005-10-21 00:00:00 -0400351 struct acpi_gpe_register_info *gpe_register_info;
Lv Zheng779ba5a2015-02-05 15:20:22 +0800352 struct acpi_gpe_event_info *gpe_event_info;
353 u32 gpe_number;
Bob Moore08978312005-10-21 00:00:00 -0400354 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500355 acpi_cpu_flags flags;
Bob Moore67a119f2008-06-10 13:42:13 +0800356 u32 i;
357 u32 j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Bob Mooreb229cf92006-04-21 17:15:00 -0400359 ACPI_FUNCTION_NAME(ev_gpe_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Check for the case where there are no GPEs */
362
363 if (!gpe_xrupt_list) {
364 return (int_status);
365 }
366
Bob Moore967440e32006-06-23 17:04:00 -0400367 /*
368 * We need to obtain the GPE lock for both the data structs and registers
Bob Moore9f15fc62008-11-12 16:01:56 +0800369 * Note: Not necessary to obtain the hardware lock, since the GPE
370 * registers are owned by the gpe_lock.
Bob Moore967440e32006-06-23 17:04:00 -0400371 */
Len Brown4be44fc2005-08-05 00:44:28 -0400372 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Bob Moore4c90ece2006-06-08 16:29:00 -0400373
374 /* Examine all GPE blocks attached to this interrupt level */
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 gpe_block = gpe_xrupt_list->gpe_block_list_head;
377 while (gpe_block) {
Lv Zheng779ba5a2015-02-05 15:20:22 +0800378 gpe_device = gpe_block->node;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800381 * Read all of the 8-bit GPE status and enable registers in this GPE
382 * block, saving all of them. Find all currently active GP events.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
384 for (i = 0; i < gpe_block->register_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Get the next status/enable pair */
387
388 gpe_register_info = &gpe_block->register_info[i];
389
Lin Ming6dfad332011-02-14 15:29:34 +0800390 /*
391 * Optimization: If there are no GPEs enabled within this
392 * register, we can safely ignore the entire register.
393 */
394 if (!(gpe_register_info->enable_for_run |
395 gpe_register_info->enable_for_wake)) {
Bob Mooree1154eb2012-08-17 10:55:14 +0800396 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
Bob Moore7505da42014-04-30 10:06:15 +0800397 "Ignore disabled registers for GPE %02X-%02X: "
Bob Mooree1154eb2012-08-17 10:55:14 +0800398 "RunEnable=%02X, WakeEnable=%02X\n",
399 gpe_register_info->
400 base_gpe_number,
401 gpe_register_info->
402 base_gpe_number +
403 (ACPI_GPE_REGISTER_WIDTH - 1),
404 gpe_register_info->
405 enable_for_run,
406 gpe_register_info->
407 enable_for_wake));
Lin Ming6dfad332011-02-14 15:29:34 +0800408 continue;
409 }
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* Now look at the individual GPEs in this byte register */
412
413 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400414
Erik Schmauss8d593492018-03-14 16:12:58 -0700415 /* Detect and dispatch one GPE bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Lv Zheng779ba5a2015-02-05 15:20:22 +0800417 gpe_event_info =
418 &gpe_block->
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800419 event_info[((acpi_size)i *
Lv Zheng779ba5a2015-02-05 15:20:22 +0800420 ACPI_GPE_REGISTER_WIDTH) + j];
421 gpe_number =
422 j + gpe_register_info->base_gpe_number;
Erik Schmauss8d593492018-03-14 16:12:58 -0700423 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
424 int_status |=
425 acpi_ev_detect_gpe(gpe_device,
426 gpe_event_info,
427 gpe_number);
428 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 }
431
432 gpe_block = gpe_block->next;
433 }
434
Len Brown4be44fc2005-08-05 00:44:28 -0400435 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return (int_status);
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*******************************************************************************
440 *
441 * FUNCTION: acpi_ev_asynch_execute_gpe_method
442 *
443 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
444 *
445 * RETURN: None
446 *
Bob Moore41195322006-05-26 16:36:00 -0400447 * DESCRIPTION: Perform the actual execution of a GPE control method. This
448 * function is called from an invocation of acpi_os_execute and
449 * therefore does NOT execute at interrupt level - so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * the control method itself is not executed in the context of
451 * an interrupt handler.
452 *
453 ******************************************************************************/
454
Len Brown4be44fc2005-08-05 00:44:28 -0400455static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Lin Ming5a284cd2010-12-13 13:39:07 +0800457 struct acpi_gpe_event_info *gpe_event_info = context;
David E. Boxb7be6882015-02-05 15:20:07 +0800458 acpi_status status = AE_OK;
Bob Moore41195322006-05-26 16:36:00 -0400459 struct acpi_evaluate_info *info;
Bob Moore5816b342012-06-29 10:04:17 +0800460 struct acpi_gpe_notify_info *notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Bob Mooreb229cf92006-04-21 17:15:00 -0400462 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Lin Mingbba63a22010-12-13 13:39:17 +0800464 /* Do the correct dispatch - normal method or implicit notify */
465
Lv Zheng7c433122015-02-05 15:20:29 +0800466 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
Lin Mingbba63a22010-12-13 13:39:17 +0800467 case ACPI_GPE_DISPATCH_NOTIFY:
Lin Mingbba63a22010-12-13 13:39:17 +0800468 /*
469 * Implicit notify.
470 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
471 * NOTE: the request is queued for execution after this method
472 * completes. The notify handlers are NOT invoked synchronously
473 * from this thread -- because handlers may in turn run other
474 * control methods.
Bob Moore5816b342012-06-29 10:04:17 +0800475 *
476 * June 2012: Expand implicit notify mechanism to support
477 * notifies on multiple device objects.
Lin Mingbba63a22010-12-13 13:39:17 +0800478 */
Lv Zhengb18da582015-02-05 15:20:01 +0800479 notify = gpe_event_info->dispatch.notify_list;
Bob Moore5816b342012-06-29 10:04:17 +0800480 while (ACPI_SUCCESS(status) && notify) {
481 status =
482 acpi_ev_queue_notify_request(notify->device_node,
483 ACPI_NOTIFY_DEVICE_WAKE);
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100484
Bob Moore5816b342012-06-29 10:04:17 +0800485 notify = notify->next;
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100486 }
487
Lin Mingbba63a22010-12-13 13:39:17 +0800488 break;
489
490 case ACPI_GPE_DISPATCH_METHOD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Bob Moore41195322006-05-26 16:36:00 -0400492 /* Allocate the evaluation information block */
493
494 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
495 if (!info) {
496 status = AE_NO_MEMORY;
497 } else {
498 /*
Lv Zheng75c80442012-12-19 05:36:49 +0000499 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
500 * _Lxx/_Exx control method that corresponds to this GPE
Bob Moore41195322006-05-26 16:36:00 -0400501 */
502 info->prefix_node =
Lv Zhengb18da582015-02-05 15:20:01 +0800503 gpe_event_info->dispatch.method_node;
Bob Moore41195322006-05-26 16:36:00 -0400504 info->flags = ACPI_IGNORE_RETURN_VALUE;
505
506 status = acpi_ns_evaluate(info);
507 ACPI_FREE(info);
508 }
509
Len Brown4be44fc2005-08-05 00:44:28 -0400510 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500511 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore2e420052007-02-02 19:48:18 +0300512 "while evaluating GPE method [%4.4s]",
Lv Zhengb18da582015-02-05 15:20:01 +0800513 acpi_ut_get_node_name(gpe_event_info->
514 dispatch.
515 method_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Lin Mingbba63a22010-12-13 13:39:17 +0800517 break;
518
519 default:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000520
Lv Zhengb18da582015-02-05 15:20:01 +0800521 goto error_exit; /* Should never happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Lin Ming5a284cd2010-12-13 13:39:07 +0800523
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300524 /* Defer enabling of GPE until all notify handlers are done */
Lin Ming5a284cd2010-12-13 13:39:07 +0800525
526 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
Lv Zhengb18da582015-02-05 15:20:01 +0800527 acpi_ev_asynch_enable_gpe, gpe_event_info);
528 if (ACPI_SUCCESS(status)) {
529 return_VOID;
Lin Ming5a284cd2010-12-13 13:39:07 +0800530 }
Lv Zhengb18da582015-02-05 15:20:01 +0800531
532error_exit:
533 acpi_ev_asynch_enable_gpe(gpe_event_info);
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300534 return_VOID;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Lin Mingbba63a22010-12-13 13:39:17 +0800537
Lin Ming5a284cd2010-12-13 13:39:07 +0800538/*******************************************************************************
539 *
540 * FUNCTION: acpi_ev_asynch_enable_gpe
541 *
542 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
543 * Callback from acpi_os_execute
544 *
545 * RETURN: None
546 *
547 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
Lin Mingbba63a22010-12-13 13:39:17 +0800548 * complete (i.e., finish execution of Notify)
Lin Ming5a284cd2010-12-13 13:39:07 +0800549 *
550 ******************************************************************************/
551
552static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300553{
554 struct acpi_gpe_event_info *gpe_event_info = context;
Lv Zheng7dd88212014-12-02 08:56:55 +0800555 acpi_cpu_flags flags;
Lin Mingbba63a22010-12-13 13:39:17 +0800556
Lv Zheng7dd88212014-12-02 08:56:55 +0800557 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Lin Mingbba63a22010-12-13 13:39:17 +0800558 (void)acpi_ev_finish_gpe(gpe_event_info);
Lv Zheng7dd88212014-12-02 08:56:55 +0800559 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Lin Mingbba63a22010-12-13 13:39:17 +0800560
Lin Mingbba63a22010-12-13 13:39:17 +0800561 return;
562}
563
564
565/*******************************************************************************
566 *
567 * FUNCTION: acpi_ev_finish_gpe
568 *
569 * PARAMETERS: gpe_event_info - Info for this GPE
570 *
571 * RETURN: Status
572 *
573 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
574 * of a GPE method or a synchronous or asynchronous GPE handler.
575 *
576 ******************************************************************************/
577
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800578acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)
Lin Mingbba63a22010-12-13 13:39:17 +0800579{
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300580 acpi_status status;
Lin Ming5a284cd2010-12-13 13:39:07 +0800581
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300582 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400583 ACPI_GPE_LEVEL_TRIGGERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800585 * GPE is level-triggered, we clear the GPE status bit after
586 * handling the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300588 status = acpi_hw_clear_gpe(gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400589 if (ACPI_FAILURE(status)) {
Lin Mingbba63a22010-12-13 13:39:17 +0800590 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 }
593
Bob Moorede5668f2010-07-06 10:30:37 +0800594 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800595 * Enable this GPE, conditionally. This means that the GPE will
Rafael J. Wysockic50f13c2014-12-01 23:50:16 +0100596 * only be physically enabled if the enable_mask bit is set
Lin Mingbba63a22010-12-13 13:39:17 +0800597 * in the event_info.
Bob Moorede5668f2010-07-06 10:30:37 +0800598 */
Lin Ming3a378982010-12-13 13:36:15 +0800599 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
Lv Zheng2af52c22016-08-04 16:43:39 +0800600 gpe_event_info->disable_for_dispatch = FALSE;
Lin Mingbba63a22010-12-13 13:39:17 +0800601 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Lin Mingbba63a22010-12-13 13:39:17 +0800604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*******************************************************************************
606 *
Erik Schmauss8d593492018-03-14 16:12:58 -0700607 * FUNCTION: acpi_ev_detect_gpe
608 *
609 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
610 * gpe_event_info - Info for this GPE
611 * gpe_number - Number relative to the parent GPE block
612 *
613 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
614 *
615 * DESCRIPTION: Detect and dispatch a General Purpose Event to either a function
616 * (e.g. EC) or method (e.g. _Lxx/_Exx) handler.
617 * NOTE: GPE is W1C, so it is possible to handle a single GPE from both
618 * task and irq context in parallel as long as the process to
619 * detect and mask the GPE is atomic.
620 * However the atomicity of ACPI_GPE_DISPATCH_RAW_HANDLER is
621 * dependent on the raw handler itself.
622 *
623 ******************************************************************************/
624
625u32
626acpi_ev_detect_gpe(struct acpi_namespace_node *gpe_device,
627 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
628{
629 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
630 u8 enabled_status_byte;
631 u64 status_reg;
632 u64 enable_reg;
633 u32 register_bit;
634 struct acpi_gpe_register_info *gpe_register_info;
635 struct acpi_gpe_handler_info *gpe_handler_info;
636 acpi_cpu_flags flags;
637 acpi_status status;
638
639 ACPI_FUNCTION_TRACE(ev_gpe_detect);
640
641 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
642
Rafael J. Wysocki74536452018-05-25 10:30:35 +0200643 if (!gpe_event_info) {
644 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
645 if (!gpe_event_info)
646 goto error_exit;
647 }
648
Erik Schmauss8d593492018-03-14 16:12:58 -0700649 /* Get the info block for the entire GPE register */
650
651 gpe_register_info = gpe_event_info->register_info;
652
653 /* Get the register bitmask for this GPE */
654
655 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
656
657 /* GPE currently enabled (enable bit == 1)? */
658
Rafael J. Wysockif06011ad2020-09-04 18:27:43 +0200659 status = acpi_hw_gpe_read(&enable_reg, &gpe_register_info->enable_address);
Erik Schmauss8d593492018-03-14 16:12:58 -0700660 if (ACPI_FAILURE(status)) {
661 goto error_exit;
662 }
663
664 /* GPE currently active (status bit == 1)? */
665
Rafael J. Wysockif06011ad2020-09-04 18:27:43 +0200666 status = acpi_hw_gpe_read(&status_reg, &gpe_register_info->status_address);
Erik Schmauss8d593492018-03-14 16:12:58 -0700667 if (ACPI_FAILURE(status)) {
668 goto error_exit;
669 }
670
671 /* Check if there is anything active at all in this GPE */
672
673 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
674 "Read registers for GPE %02X: Status=%02X, Enable=%02X, "
675 "RunEnable=%02X, WakeEnable=%02X\n",
676 gpe_number,
677 (u32)(status_reg & register_bit),
678 (u32)(enable_reg & register_bit),
679 gpe_register_info->enable_for_run,
680 gpe_register_info->enable_for_wake));
681
682 enabled_status_byte = (u8)(status_reg & enable_reg);
683 if (!(enabled_status_byte & register_bit)) {
684 goto error_exit;
685 }
686
687 /* Invoke global event handler if present */
688
689 acpi_gpe_count++;
690 if (acpi_gbl_global_event_handler) {
691 acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE,
692 gpe_device, gpe_number,
693 acpi_gbl_global_event_handler_context);
694 }
695
696 /* Found an active GPE */
697
698 if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
699 ACPI_GPE_DISPATCH_RAW_HANDLER) {
700
701 /* Dispatch the event to a raw handler */
702
703 gpe_handler_info = gpe_event_info->dispatch.handler;
704
705 /*
706 * There is no protection around the namespace node
707 * and the GPE handler to ensure a safe destruction
708 * because:
709 * 1. The namespace node is expected to always
710 * exist after loading a table.
711 * 2. The GPE handler is expected to be flushed by
712 * acpi_os_wait_events_complete() before the
713 * destruction.
714 */
715 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
716 int_status |=
717 gpe_handler_info->address(gpe_device, gpe_number,
718 gpe_handler_info->context);
719 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
720 } else {
721 /* Dispatch the event to a standard handler or method. */
722
723 int_status |= acpi_ev_gpe_dispatch(gpe_device,
724 gpe_event_info, gpe_number);
725 }
726
727error_exit:
728 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
729 return (int_status);
730}
731
732/*******************************************************************************
733 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * FUNCTION: acpi_ev_gpe_dispatch
735 *
Lv Zhengf19f1a72014-10-10 10:39:39 +0800736 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
737 * gpe_event_info - Info for this GPE
738 * gpe_number - Number relative to the parent GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 *
740 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
741 *
742 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
743 * or method (e.g. _Lxx/_Exx) handler.
744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ******************************************************************************/
746
747u32
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800748acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
Lv Zhengf19f1a72014-10-10 10:39:39 +0800749 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Len Brown4be44fc2005-08-05 00:44:28 -0400751 acpi_status status;
Lin Mingbba63a22010-12-13 13:39:17 +0800752 u32 return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Bob Mooreb229cf92006-04-21 17:15:00 -0400754 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800757 * Always disable the GPE so that it does not keep firing before
758 * any asynchronous activity completes (either from the execution
759 * of a GPE method or an asynchronous GPE handler.)
760 *
761 * If there is no handler or method to run, just disable the
762 * GPE and leave it disabled permanently to prevent further such
763 * pointless events from firing.
764 */
765 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
766 if (ACPI_FAILURE(status)) {
767 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore7505da42014-04-30 10:06:15 +0800768 "Unable to disable GPE %02X", gpe_number));
Bob Moorefd1af712013-03-08 09:22:23 +0000769 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Lin Mingbba63a22010-12-13 13:39:17 +0800770 }
771
772 /*
Lv Zheng6ec5e122014-06-15 08:42:31 +0800773 * If edge-triggered, clear the GPE status bit now. Note that
774 * level-triggered events are cleared after the GPE is serviced.
775 */
776 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
777 ACPI_GPE_EDGE_TRIGGERED) {
778 status = acpi_hw_clear_gpe(gpe_event_info);
779 if (ACPI_FAILURE(status)) {
780 ACPI_EXCEPTION((AE_INFO, status,
781 "Unable to clear GPE %02X",
782 gpe_number));
783 (void)acpi_hw_low_set_gpe(gpe_event_info,
784 ACPI_GPE_CONDITIONAL_ENABLE);
785 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
786 }
787 }
788
Lv Zheng2af52c22016-08-04 16:43:39 +0800789 gpe_event_info->disable_for_dispatch = TRUE;
790
Lv Zheng6ec5e122014-06-15 08:42:31 +0800791 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800792 * Dispatch the GPE to either an installed handler or the control
793 * method associated with this GPE (_Lxx or _Exx). If a handler
794 * exists, we invoke it and do not attempt to run the method.
795 * If there is neither a handler nor a method, leave the GPE
796 * disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Lv Zheng7c433122015-02-05 15:20:29 +0800798 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 case ACPI_GPE_DISPATCH_HANDLER:
800
Lin Mingbba63a22010-12-13 13:39:17 +0800801 /* Invoke the installed handler (at interrupt level) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Lin Mingbba63a22010-12-13 13:39:17 +0800803 return_value =
804 gpe_event_info->dispatch.handler->address(gpe_device,
805 gpe_number,
806 gpe_event_info->
807 dispatch.handler->
808 context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800810 /* If requested, clear (if level-triggered) and re-enable the GPE */
Lin Mingbba63a22010-12-13 13:39:17 +0800811
812 if (return_value & ACPI_REENABLE_GPE) {
813 (void)acpi_ev_finish_gpe(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 break;
816
817 case ACPI_GPE_DISPATCH_METHOD:
Lin Mingbba63a22010-12-13 13:39:17 +0800818 case ACPI_GPE_DISPATCH_NOTIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /*
820 * Execute the method associated with the GPE
821 * NOTE: Level-triggered GPEs are cleared after the method completes.
822 */
Bob Moore958dd242006-05-12 17:12:00 -0400823 status = acpi_os_execute(OSL_GPE_HANDLER,
824 acpi_ev_asynch_execute_gpe_method,
825 gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400826 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500827 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore7505da42014-04-30 10:06:15 +0800828 "Unable to queue handler for GPE %02X - event disabled",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500829 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 break;
832
833 default:
Lin Ming0f849d22010-04-06 14:52:37 +0800834 /*
835 * No handler or method to run!
836 * 03/2010: This case should no longer be possible. We will not allow
837 * a GPE to be enabled if it has no handler or method.
838 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500839 ACPI_ERROR((AE_INFO,
Bob Moore7505da42014-04-30 10:06:15 +0800840 "No handler or method for GPE %02X, disabling event",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500841 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 break;
844 }
845
Bob Moorefd1af712013-03-08 09:22:23 +0000846 return_UINT32(ACPI_INTERRUPT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Bob Moore33620c52012-02-14 18:14:27 +0800848
849#endif /* !ACPI_REDUCED_HARDWARE */