blob: e10fec99a182eca363167e27e1a6338e42586c09 [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 Mooreda6f8322018-01-04 10:06:38 -08006 * Copyright (C) 2000 - 2018, 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
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080084 /* Enable the requested GPE */
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080085
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. Wysocki28f4f8a2010-08-03 23:55:14 +0200149 *
150 * RETURN: Status
151 *
152 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
153 * hardware-enabled.
154 *
155 ******************************************************************************/
156
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000157acpi_status
158acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200159{
160 acpi_status status = AE_OK;
161
Lin Ming3a378982010-12-13 13:36:15 +0800162 ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
163
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200164 if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
165 return_ACPI_STATUS(AE_LIMIT);
166 }
167
168 gpe_event_info->runtime_count++;
169 if (gpe_event_info->runtime_count == 1) {
Lin Mingda503372010-12-13 13:39:37 +0800170
171 /* Enable on first reference */
172
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200173 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
174 if (ACPI_SUCCESS(status)) {
175 status = acpi_ev_enable_gpe(gpe_event_info);
176 }
177
178 if (ACPI_FAILURE(status)) {
179 gpe_event_info->runtime_count--;
180 }
181 }
182
183 return_ACPI_STATUS(status);
184}
185
186/*******************************************************************************
187 *
Lin Ming3a378982010-12-13 13:36:15 +0800188 * FUNCTION: acpi_ev_remove_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200189 *
Lin Mingda503372010-12-13 13:39:37 +0800190 * PARAMETERS: gpe_event_info - Remove a reference to this GPE
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200191 *
192 * RETURN: Status
193 *
194 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
195 * removed, the GPE is hardware-disabled.
196 *
197 ******************************************************************************/
198
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000199acpi_status
200acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200201{
202 acpi_status status = AE_OK;
203
Lin Ming3a378982010-12-13 13:36:15 +0800204 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
205
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200206 if (!gpe_event_info->runtime_count) {
207 return_ACPI_STATUS(AE_LIMIT);
208 }
209
210 gpe_event_info->runtime_count--;
211 if (!gpe_event_info->runtime_count) {
Lin Mingda503372010-12-13 13:39:37 +0800212
213 /* Disable on last reference */
214
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200215 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
216 if (ACPI_SUCCESS(status)) {
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000217 status =
218 acpi_hw_low_set_gpe(gpe_event_info,
Rafael J. Wysocki0ee0d342015-04-15 04:00:27 +0200219 ACPI_GPE_DISABLE);
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200220 }
221
222 if (ACPI_FAILURE(status)) {
223 gpe_event_info->runtime_count++;
224 }
225 }
226
227 return_ACPI_STATUS(status);
228}
229
230/*******************************************************************************
231 *
Lin Ming0f849d22010-04-06 14:52:37 +0800232 * FUNCTION: acpi_ev_low_get_gpe_info
233 *
234 * PARAMETERS: gpe_number - Raw GPE number
235 * gpe_block - A GPE info block
236 *
237 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
238 * is not within the specified GPE block)
239 *
240 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
241 * the low-level implementation of ev_get_gpe_event_info.
242 *
243 ******************************************************************************/
244
245struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
246 struct acpi_gpe_block_info
247 *gpe_block)
248{
249 u32 gpe_index;
250
251 /*
252 * Validate that the gpe_number is within the specified gpe_block.
253 * (Two steps)
254 */
255 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
256 return (NULL);
257 }
258
259 gpe_index = gpe_number - gpe_block->block_base_number;
260 if (gpe_index >= gpe_block->gpe_count) {
261 return (NULL);
262 }
263
264 return (&gpe_block->event_info[gpe_index]);
265}
266
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/*******************************************************************************
269 *
270 * FUNCTION: acpi_ev_get_gpe_event_info
271 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800272 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * gpe_number - Raw GPE number
274 *
275 * RETURN: A GPE event_info struct. NULL if not a valid GPE
276 *
277 * DESCRIPTION: Returns the event_info struct associated with this GPE.
278 * Validates the gpe_block and the gpe_number
279 *
280 * Should be called only when the GPE lists are semaphore locked
281 * and not subject to change.
282 *
283 ******************************************************************************/
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
286 u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Len Brown4be44fc2005-08-05 00:44:28 -0400288 union acpi_operand_object *obj_desc;
Lin Ming0f849d22010-04-06 14:52:37 +0800289 struct acpi_gpe_event_info *gpe_info;
Bob Moore67a119f2008-06-10 13:42:13 +0800290 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Len Brown4be44fc2005-08-05 00:44:28 -0400292 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Bob Moore186c3072010-04-27 11:32:28 +0800294 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (!gpe_device) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
299
300 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
Lin Ming0f849d22010-04-06 14:52:37 +0800301 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
302 acpi_gbl_gpe_fadt_blocks
303 [i]);
304 if (gpe_info) {
305 return (gpe_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307 }
308
309 /* The gpe_number was not in the range of either FADT GPE block */
310
311 return (NULL);
312 }
313
314 /* A Non-NULL gpe_device means this is a GPE Block Device */
315
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000316 obj_desc =
317 acpi_ns_get_attached_object((struct acpi_namespace_node *)
Len Brownfd350942007-05-09 23:34:35 -0400318 gpe_device);
Len Brown4be44fc2005-08-05 00:44:28 -0400319 if (!obj_desc || !obj_desc->device.gpe_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return (NULL);
321 }
322
Lin Ming0f849d22010-04-06 14:52:37 +0800323 return (acpi_ev_low_get_gpe_info
324 (gpe_number, obj_desc->device.gpe_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*******************************************************************************
328 *
329 * FUNCTION: acpi_ev_gpe_detect
330 *
331 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
332 * Can have multiple GPE blocks attached.
333 *
334 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
335 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800336 * DESCRIPTION: Detect if any GP events have occurred. This function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * executed at interrupt level.
338 *
339 ******************************************************************************/
340
Lv Zhengf19f1a72014-10-10 10:39:39 +0800341u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Len Brown4be44fc2005-08-05 00:44:28 -0400343 struct acpi_gpe_block_info *gpe_block;
Lv Zheng779ba5a2015-02-05 15:20:22 +0800344 struct acpi_namespace_node *gpe_device;
Bob Moore08978312005-10-21 00:00:00 -0400345 struct acpi_gpe_register_info *gpe_register_info;
Lv Zheng779ba5a2015-02-05 15:20:22 +0800346 struct acpi_gpe_event_info *gpe_event_info;
347 u32 gpe_number;
Bob Moore08978312005-10-21 00:00:00 -0400348 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500349 acpi_cpu_flags flags;
Bob Moore67a119f2008-06-10 13:42:13 +0800350 u32 i;
351 u32 j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Bob Mooreb229cf92006-04-21 17:15:00 -0400353 ACPI_FUNCTION_NAME(ev_gpe_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Check for the case where there are no GPEs */
356
357 if (!gpe_xrupt_list) {
358 return (int_status);
359 }
360
Bob Moore967440e32006-06-23 17:04:00 -0400361 /*
362 * We need to obtain the GPE lock for both the data structs and registers
Bob Moore9f15fc62008-11-12 16:01:56 +0800363 * Note: Not necessary to obtain the hardware lock, since the GPE
364 * registers are owned by the gpe_lock.
Bob Moore967440e32006-06-23 17:04:00 -0400365 */
Len Brown4be44fc2005-08-05 00:44:28 -0400366 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Bob Moore4c90ece2006-06-08 16:29:00 -0400367
368 /* Examine all GPE blocks attached to this interrupt level */
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 gpe_block = gpe_xrupt_list->gpe_block_list_head;
371 while (gpe_block) {
Lv Zheng779ba5a2015-02-05 15:20:22 +0800372 gpe_device = gpe_block->node;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800375 * Read all of the 8-bit GPE status and enable registers in this GPE
376 * block, saving all of them. Find all currently active GP events.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
378 for (i = 0; i < gpe_block->register_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Get the next status/enable pair */
381
382 gpe_register_info = &gpe_block->register_info[i];
383
Lin Ming6dfad332011-02-14 15:29:34 +0800384 /*
385 * Optimization: If there are no GPEs enabled within this
386 * register, we can safely ignore the entire register.
387 */
388 if (!(gpe_register_info->enable_for_run |
389 gpe_register_info->enable_for_wake)) {
Bob Mooree1154eb2012-08-17 10:55:14 +0800390 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
Bob Moore7505da42014-04-30 10:06:15 +0800391 "Ignore disabled registers for GPE %02X-%02X: "
Bob Mooree1154eb2012-08-17 10:55:14 +0800392 "RunEnable=%02X, WakeEnable=%02X\n",
393 gpe_register_info->
394 base_gpe_number,
395 gpe_register_info->
396 base_gpe_number +
397 (ACPI_GPE_REGISTER_WIDTH - 1),
398 gpe_register_info->
399 enable_for_run,
400 gpe_register_info->
401 enable_for_wake));
Lin Ming6dfad332011-02-14 15:29:34 +0800402 continue;
403 }
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* Now look at the individual GPEs in this byte register */
406
407 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400408
Erik Schmauss8d593492018-03-14 16:12:58 -0700409 /* Detect and dispatch one GPE bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Lv Zheng779ba5a2015-02-05 15:20:22 +0800411 gpe_event_info =
412 &gpe_block->
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800413 event_info[((acpi_size)i *
Lv Zheng779ba5a2015-02-05 15:20:22 +0800414 ACPI_GPE_REGISTER_WIDTH) + j];
415 gpe_number =
416 j + gpe_register_info->base_gpe_number;
Erik Schmauss8d593492018-03-14 16:12:58 -0700417 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
418 int_status |=
419 acpi_ev_detect_gpe(gpe_device,
420 gpe_event_info,
421 gpe_number);
422 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 }
425
426 gpe_block = gpe_block->next;
427 }
428
Len Brown4be44fc2005-08-05 00:44:28 -0400429 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return (int_status);
431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/*******************************************************************************
434 *
435 * FUNCTION: acpi_ev_asynch_execute_gpe_method
436 *
437 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
438 *
439 * RETURN: None
440 *
Bob Moore41195322006-05-26 16:36:00 -0400441 * DESCRIPTION: Perform the actual execution of a GPE control method. This
442 * function is called from an invocation of acpi_os_execute and
443 * therefore does NOT execute at interrupt level - so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * the control method itself is not executed in the context of
445 * an interrupt handler.
446 *
447 ******************************************************************************/
448
Len Brown4be44fc2005-08-05 00:44:28 -0400449static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Lin Ming5a284cd2010-12-13 13:39:07 +0800451 struct acpi_gpe_event_info *gpe_event_info = context;
David E. Boxb7be6882015-02-05 15:20:07 +0800452 acpi_status status = AE_OK;
Bob Moore41195322006-05-26 16:36:00 -0400453 struct acpi_evaluate_info *info;
Bob Moore5816b342012-06-29 10:04:17 +0800454 struct acpi_gpe_notify_info *notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Bob Mooreb229cf92006-04-21 17:15:00 -0400456 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Lin Mingbba63a22010-12-13 13:39:17 +0800458 /* Do the correct dispatch - normal method or implicit notify */
459
Lv Zheng7c433122015-02-05 15:20:29 +0800460 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
Lin Mingbba63a22010-12-13 13:39:17 +0800461 case ACPI_GPE_DISPATCH_NOTIFY:
Lin Mingbba63a22010-12-13 13:39:17 +0800462 /*
463 * Implicit notify.
464 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
465 * NOTE: the request is queued for execution after this method
466 * completes. The notify handlers are NOT invoked synchronously
467 * from this thread -- because handlers may in turn run other
468 * control methods.
Bob Moore5816b342012-06-29 10:04:17 +0800469 *
470 * June 2012: Expand implicit notify mechanism to support
471 * notifies on multiple device objects.
Lin Mingbba63a22010-12-13 13:39:17 +0800472 */
Lv Zhengb18da582015-02-05 15:20:01 +0800473 notify = gpe_event_info->dispatch.notify_list;
Bob Moore5816b342012-06-29 10:04:17 +0800474 while (ACPI_SUCCESS(status) && notify) {
475 status =
476 acpi_ev_queue_notify_request(notify->device_node,
477 ACPI_NOTIFY_DEVICE_WAKE);
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100478
Bob Moore5816b342012-06-29 10:04:17 +0800479 notify = notify->next;
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100480 }
481
Lin Mingbba63a22010-12-13 13:39:17 +0800482 break;
483
484 case ACPI_GPE_DISPATCH_METHOD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Bob Moore41195322006-05-26 16:36:00 -0400486 /* Allocate the evaluation information block */
487
488 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
489 if (!info) {
490 status = AE_NO_MEMORY;
491 } else {
492 /*
Lv Zheng75c80442012-12-19 05:36:49 +0000493 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
494 * _Lxx/_Exx control method that corresponds to this GPE
Bob Moore41195322006-05-26 16:36:00 -0400495 */
496 info->prefix_node =
Lv Zhengb18da582015-02-05 15:20:01 +0800497 gpe_event_info->dispatch.method_node;
Bob Moore41195322006-05-26 16:36:00 -0400498 info->flags = ACPI_IGNORE_RETURN_VALUE;
499
500 status = acpi_ns_evaluate(info);
501 ACPI_FREE(info);
502 }
503
Len Brown4be44fc2005-08-05 00:44:28 -0400504 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500505 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore2e420052007-02-02 19:48:18 +0300506 "while evaluating GPE method [%4.4s]",
Lv Zhengb18da582015-02-05 15:20:01 +0800507 acpi_ut_get_node_name(gpe_event_info->
508 dispatch.
509 method_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Lin Mingbba63a22010-12-13 13:39:17 +0800511 break;
512
513 default:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000514
Lv Zhengb18da582015-02-05 15:20:01 +0800515 goto error_exit; /* Should never happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Lin Ming5a284cd2010-12-13 13:39:07 +0800517
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300518 /* Defer enabling of GPE until all notify handlers are done */
Lin Ming5a284cd2010-12-13 13:39:07 +0800519
520 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
Lv Zhengb18da582015-02-05 15:20:01 +0800521 acpi_ev_asynch_enable_gpe, gpe_event_info);
522 if (ACPI_SUCCESS(status)) {
523 return_VOID;
Lin Ming5a284cd2010-12-13 13:39:07 +0800524 }
Lv Zhengb18da582015-02-05 15:20:01 +0800525
526error_exit:
527 acpi_ev_asynch_enable_gpe(gpe_event_info);
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300528 return_VOID;
529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Lin Mingbba63a22010-12-13 13:39:17 +0800531
Lin Ming5a284cd2010-12-13 13:39:07 +0800532/*******************************************************************************
533 *
534 * FUNCTION: acpi_ev_asynch_enable_gpe
535 *
536 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
537 * Callback from acpi_os_execute
538 *
539 * RETURN: None
540 *
541 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
Lin Mingbba63a22010-12-13 13:39:17 +0800542 * complete (i.e., finish execution of Notify)
Lin Ming5a284cd2010-12-13 13:39:07 +0800543 *
544 ******************************************************************************/
545
546static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300547{
548 struct acpi_gpe_event_info *gpe_event_info = context;
Lv Zheng7dd88212014-12-02 08:56:55 +0800549 acpi_cpu_flags flags;
Lin Mingbba63a22010-12-13 13:39:17 +0800550
Lv Zheng7dd88212014-12-02 08:56:55 +0800551 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Lin Mingbba63a22010-12-13 13:39:17 +0800552 (void)acpi_ev_finish_gpe(gpe_event_info);
Lv Zheng7dd88212014-12-02 08:56:55 +0800553 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Lin Mingbba63a22010-12-13 13:39:17 +0800554
Lin Mingbba63a22010-12-13 13:39:17 +0800555 return;
556}
557
558
559/*******************************************************************************
560 *
561 * FUNCTION: acpi_ev_finish_gpe
562 *
563 * PARAMETERS: gpe_event_info - Info for this GPE
564 *
565 * RETURN: Status
566 *
567 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
568 * of a GPE method or a synchronous or asynchronous GPE handler.
569 *
570 ******************************************************************************/
571
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800572acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)
Lin Mingbba63a22010-12-13 13:39:17 +0800573{
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300574 acpi_status status;
Lin Ming5a284cd2010-12-13 13:39:07 +0800575
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300576 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400577 ACPI_GPE_LEVEL_TRIGGERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800579 * GPE is level-triggered, we clear the GPE status bit after
580 * handling the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300582 status = acpi_hw_clear_gpe(gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400583 if (ACPI_FAILURE(status)) {
Lin Mingbba63a22010-12-13 13:39:17 +0800584 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 }
587
Bob Moorede5668f2010-07-06 10:30:37 +0800588 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800589 * Enable this GPE, conditionally. This means that the GPE will
Rafael J. Wysockic50f13c2014-12-01 23:50:16 +0100590 * only be physically enabled if the enable_mask bit is set
Lin Mingbba63a22010-12-13 13:39:17 +0800591 * in the event_info.
Bob Moorede5668f2010-07-06 10:30:37 +0800592 */
Lin Ming3a378982010-12-13 13:36:15 +0800593 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
Lv Zheng2af52c22016-08-04 16:43:39 +0800594 gpe_event_info->disable_for_dispatch = FALSE;
Lin Mingbba63a22010-12-13 13:39:17 +0800595 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Lin Mingbba63a22010-12-13 13:39:17 +0800598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*******************************************************************************
600 *
Erik Schmauss8d593492018-03-14 16:12:58 -0700601 * FUNCTION: acpi_ev_detect_gpe
602 *
603 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
604 * gpe_event_info - Info for this GPE
605 * gpe_number - Number relative to the parent GPE block
606 *
607 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
608 *
609 * DESCRIPTION: Detect and dispatch a General Purpose Event to either a function
610 * (e.g. EC) or method (e.g. _Lxx/_Exx) handler.
611 * NOTE: GPE is W1C, so it is possible to handle a single GPE from both
612 * task and irq context in parallel as long as the process to
613 * detect and mask the GPE is atomic.
614 * However the atomicity of ACPI_GPE_DISPATCH_RAW_HANDLER is
615 * dependent on the raw handler itself.
616 *
617 ******************************************************************************/
618
619u32
620acpi_ev_detect_gpe(struct acpi_namespace_node *gpe_device,
621 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
622{
623 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
624 u8 enabled_status_byte;
625 u64 status_reg;
626 u64 enable_reg;
627 u32 register_bit;
628 struct acpi_gpe_register_info *gpe_register_info;
629 struct acpi_gpe_handler_info *gpe_handler_info;
630 acpi_cpu_flags flags;
631 acpi_status status;
632
633 ACPI_FUNCTION_TRACE(ev_gpe_detect);
634
635 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
636
Rafael J. Wysocki74536452018-05-25 10:30:35 +0200637 if (!gpe_event_info) {
638 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
639 if (!gpe_event_info)
640 goto error_exit;
641 }
642
Erik Schmauss8d593492018-03-14 16:12:58 -0700643 /* Get the info block for the entire GPE register */
644
645 gpe_register_info = gpe_event_info->register_info;
646
647 /* Get the register bitmask for this GPE */
648
649 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
650
651 /* GPE currently enabled (enable bit == 1)? */
652
653 status = acpi_hw_read(&enable_reg, &gpe_register_info->enable_address);
654 if (ACPI_FAILURE(status)) {
655 goto error_exit;
656 }
657
658 /* GPE currently active (status bit == 1)? */
659
660 status = acpi_hw_read(&status_reg, &gpe_register_info->status_address);
661 if (ACPI_FAILURE(status)) {
662 goto error_exit;
663 }
664
665 /* Check if there is anything active at all in this GPE */
666
667 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
668 "Read registers for GPE %02X: Status=%02X, Enable=%02X, "
669 "RunEnable=%02X, WakeEnable=%02X\n",
670 gpe_number,
671 (u32)(status_reg & register_bit),
672 (u32)(enable_reg & register_bit),
673 gpe_register_info->enable_for_run,
674 gpe_register_info->enable_for_wake));
675
676 enabled_status_byte = (u8)(status_reg & enable_reg);
677 if (!(enabled_status_byte & register_bit)) {
678 goto error_exit;
679 }
680
681 /* Invoke global event handler if present */
682
683 acpi_gpe_count++;
684 if (acpi_gbl_global_event_handler) {
685 acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE,
686 gpe_device, gpe_number,
687 acpi_gbl_global_event_handler_context);
688 }
689
690 /* Found an active GPE */
691
692 if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
693 ACPI_GPE_DISPATCH_RAW_HANDLER) {
694
695 /* Dispatch the event to a raw handler */
696
697 gpe_handler_info = gpe_event_info->dispatch.handler;
698
699 /*
700 * There is no protection around the namespace node
701 * and the GPE handler to ensure a safe destruction
702 * because:
703 * 1. The namespace node is expected to always
704 * exist after loading a table.
705 * 2. The GPE handler is expected to be flushed by
706 * acpi_os_wait_events_complete() before the
707 * destruction.
708 */
709 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
710 int_status |=
711 gpe_handler_info->address(gpe_device, gpe_number,
712 gpe_handler_info->context);
713 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
714 } else {
715 /* Dispatch the event to a standard handler or method. */
716
717 int_status |= acpi_ev_gpe_dispatch(gpe_device,
718 gpe_event_info, gpe_number);
719 }
720
721error_exit:
722 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
723 return (int_status);
724}
725
726/*******************************************************************************
727 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * FUNCTION: acpi_ev_gpe_dispatch
729 *
Lv Zhengf19f1a72014-10-10 10:39:39 +0800730 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
731 * gpe_event_info - Info for this GPE
732 * gpe_number - Number relative to the parent GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
734 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
735 *
736 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
737 * or method (e.g. _Lxx/_Exx) handler.
738 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ******************************************************************************/
740
741u32
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800742acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
Lv Zhengf19f1a72014-10-10 10:39:39 +0800743 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Len Brown4be44fc2005-08-05 00:44:28 -0400745 acpi_status status;
Lin Mingbba63a22010-12-13 13:39:17 +0800746 u32 return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Bob Mooreb229cf92006-04-21 17:15:00 -0400748 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800751 * Always disable the GPE so that it does not keep firing before
752 * any asynchronous activity completes (either from the execution
753 * of a GPE method or an asynchronous GPE handler.)
754 *
755 * If there is no handler or method to run, just disable the
756 * GPE and leave it disabled permanently to prevent further such
757 * pointless events from firing.
758 */
759 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
760 if (ACPI_FAILURE(status)) {
761 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore7505da42014-04-30 10:06:15 +0800762 "Unable to disable GPE %02X", gpe_number));
Bob Moorefd1af712013-03-08 09:22:23 +0000763 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Lin Mingbba63a22010-12-13 13:39:17 +0800764 }
765
766 /*
Lv Zheng6ec5e122014-06-15 08:42:31 +0800767 * If edge-triggered, clear the GPE status bit now. Note that
768 * level-triggered events are cleared after the GPE is serviced.
769 */
770 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
771 ACPI_GPE_EDGE_TRIGGERED) {
772 status = acpi_hw_clear_gpe(gpe_event_info);
773 if (ACPI_FAILURE(status)) {
774 ACPI_EXCEPTION((AE_INFO, status,
775 "Unable to clear GPE %02X",
776 gpe_number));
777 (void)acpi_hw_low_set_gpe(gpe_event_info,
778 ACPI_GPE_CONDITIONAL_ENABLE);
779 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
780 }
781 }
782
Lv Zheng2af52c22016-08-04 16:43:39 +0800783 gpe_event_info->disable_for_dispatch = TRUE;
784
Lv Zheng6ec5e122014-06-15 08:42:31 +0800785 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800786 * Dispatch the GPE to either an installed handler or the control
787 * method associated with this GPE (_Lxx or _Exx). If a handler
788 * exists, we invoke it and do not attempt to run the method.
789 * If there is neither a handler nor a method, leave the GPE
790 * disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Lv Zheng7c433122015-02-05 15:20:29 +0800792 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 case ACPI_GPE_DISPATCH_HANDLER:
794
Lin Mingbba63a22010-12-13 13:39:17 +0800795 /* Invoke the installed handler (at interrupt level) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Lin Mingbba63a22010-12-13 13:39:17 +0800797 return_value =
798 gpe_event_info->dispatch.handler->address(gpe_device,
799 gpe_number,
800 gpe_event_info->
801 dispatch.handler->
802 context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Lin Mingbba63a22010-12-13 13:39:17 +0800804 /* If requested, clear (if level-triggered) and reenable the GPE */
805
806 if (return_value & ACPI_REENABLE_GPE) {
807 (void)acpi_ev_finish_gpe(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 break;
810
811 case ACPI_GPE_DISPATCH_METHOD:
Lin Mingbba63a22010-12-13 13:39:17 +0800812 case ACPI_GPE_DISPATCH_NOTIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Execute the method associated with the GPE
815 * NOTE: Level-triggered GPEs are cleared after the method completes.
816 */
Bob Moore958dd242006-05-12 17:12:00 -0400817 status = acpi_os_execute(OSL_GPE_HANDLER,
818 acpi_ev_asynch_execute_gpe_method,
819 gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400820 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500821 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore7505da42014-04-30 10:06:15 +0800822 "Unable to queue handler for GPE %02X - event disabled",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500823 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 break;
826
827 default:
Lin Ming0f849d22010-04-06 14:52:37 +0800828 /*
829 * No handler or method to run!
830 * 03/2010: This case should no longer be possible. We will not allow
831 * a GPE to be enabled if it has no handler or method.
832 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500833 ACPI_ERROR((AE_INFO,
Bob Moore7505da42014-04-30 10:06:15 +0800834 "No handler or method for GPE %02X, disabling event",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500835 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838 }
839
Bob Moorefd1af712013-03-08 09:22:23 +0000840 return_UINT32(ACPI_INTERRUPT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
Bob Moore33620c52012-02-14 18:14:27 +0800842
843#endif /* !ACPI_REDUCED_HARDWARE */