blob: 3bcf5ef74c6252ff9f3c73b264504a975b48da59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: evgpe - General Purpose Event handling and dispatch
4 *
5 *****************************************************************************/
6
7/*
Bob Moorea8357b02010-01-22 19:07:36 +08008 * Copyright (C) 2000 - 2010, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acevents.h"
47#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define _COMPONENT ACPI_EVENTS
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("evgpe")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Robert Moore44f6c012005-04-18 22:49:35 -040052/* Local prototypes */
Len Brown4be44fc2005-08-05 00:44:28 -040053static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Lin Ming5a284cd2010-12-13 13:39:07 +080055static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*******************************************************************************
58 *
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080059 * FUNCTION: acpi_ev_update_gpe_enable_mask
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 * PARAMETERS: gpe_event_info - GPE to update
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 * RETURN: Status
64 *
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080065 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
66 * runtime references to this GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *
68 ******************************************************************************/
69
70acpi_status
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080071acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Len Brown4be44fc2005-08-05 00:44:28 -040073 struct acpi_gpe_register_info *gpe_register_info;
Rafael J. Wysockie4e9a732010-06-08 10:48:26 +020074 u32 register_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080076 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 gpe_register_info = gpe_event_info->register_info;
79 if (!gpe_register_info) {
Len Brown4be44fc2005-08-05 00:44:28 -040080 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Bob Moored4913dc2009-03-06 10:05:18 +080082
Lin Mingb76df672010-07-01 10:07:17 +080083 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info,
Rafael J. Wysockie4e9a732010-06-08 10:48:26 +020084 gpe_register_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080086 /* Clear the run bit up front */
Lin Ming0f849d22010-04-06 14:52:37 +080087
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +010088 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080090 /* Set the mask bit only if there are references to this GPE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Lin Ming0f849d22010-04-06 14:52:37 +080092 if (gpe_event_info->runtime_count) {
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080093 ACPI_SET_BIT(gpe_register_info->enable_for_run, (u8)register_bit);
Lin Ming0f849d22010-04-06 14:52:37 +080094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Len Brown4be44fc2005-08-05 00:44:28 -040096 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +080099/*******************************************************************************
100 *
101 * FUNCTION: acpi_ev_enable_gpe
102 *
103 * PARAMETERS: gpe_event_info - GPE to enable
104 *
105 * RETURN: Status
106 *
107 * DESCRIPTION: Clear the given GPE from stale events and enable it.
108 *
109 ******************************************************************************/
110acpi_status
111acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
112{
113 acpi_status status;
114
115 ACPI_FUNCTION_TRACE(ev_enable_gpe);
116
117 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800118 * We will only allow a GPE to be enabled if it has either an associated
119 * method (_Lxx/_Exx) or a handler, or is using the implicit notify
120 * feature. Otherwise, the GPE will be immediately disabled by
121 * acpi_ev_gpe_dispatch the first time it fires.
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800122 */
Lin Mingbba63a22010-12-13 13:39:17 +0800123 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
124 ACPI_GPE_DISPATCH_NONE) {
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800125 return_ACPI_STATUS(AE_NO_HANDLER);
126 }
127
128 /* Clear the GPE (of stale events) */
129 status = acpi_hw_clear_gpe(gpe_event_info);
130 if (ACPI_FAILURE(status)) {
131 return_ACPI_STATUS(status);
132 }
133
134 /* Enable the requested GPE */
135 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
136
137 return_ACPI_STATUS(status);
138}
139
Lin Ming0f849d22010-04-06 14:52:37 +0800140
141/*******************************************************************************
142 *
Lin Ming3a378982010-12-13 13:36:15 +0800143 * FUNCTION: acpi_ev_add_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200144 *
145 * PARAMETERS: gpe_event_info - GPE to enable
146 *
147 * RETURN: Status
148 *
149 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
150 * hardware-enabled.
151 *
152 ******************************************************************************/
153
Lin Ming3a378982010-12-13 13:36:15 +0800154acpi_status acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200155{
156 acpi_status status = AE_OK;
157
Lin Ming3a378982010-12-13 13:36:15 +0800158 ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
159
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200160 if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
161 return_ACPI_STATUS(AE_LIMIT);
162 }
163
164 gpe_event_info->runtime_count++;
165 if (gpe_event_info->runtime_count == 1) {
166 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
167 if (ACPI_SUCCESS(status)) {
168 status = acpi_ev_enable_gpe(gpe_event_info);
169 }
170
171 if (ACPI_FAILURE(status)) {
172 gpe_event_info->runtime_count--;
173 }
174 }
175
176 return_ACPI_STATUS(status);
177}
178
179/*******************************************************************************
180 *
Lin Ming3a378982010-12-13 13:36:15 +0800181 * FUNCTION: acpi_ev_remove_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200182 *
183 * PARAMETERS: gpe_event_info - GPE to disable
184 *
185 * RETURN: Status
186 *
187 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
188 * removed, the GPE is hardware-disabled.
189 *
190 ******************************************************************************/
191
Lin Ming3a378982010-12-13 13:36:15 +0800192acpi_status acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200193{
194 acpi_status status = AE_OK;
195
Lin Ming3a378982010-12-13 13:36:15 +0800196 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
197
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200198 if (!gpe_event_info->runtime_count) {
199 return_ACPI_STATUS(AE_LIMIT);
200 }
201
202 gpe_event_info->runtime_count--;
203 if (!gpe_event_info->runtime_count) {
204 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
205 if (ACPI_SUCCESS(status)) {
206 status = acpi_hw_low_set_gpe(gpe_event_info,
207 ACPI_GPE_DISABLE);
208 }
209
210 if (ACPI_FAILURE(status)) {
211 gpe_event_info->runtime_count++;
212 }
213 }
214
215 return_ACPI_STATUS(status);
216}
217
218/*******************************************************************************
219 *
Lin Ming0f849d22010-04-06 14:52:37 +0800220 * FUNCTION: acpi_ev_low_get_gpe_info
221 *
222 * PARAMETERS: gpe_number - Raw GPE number
223 * gpe_block - A GPE info block
224 *
225 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
226 * is not within the specified GPE block)
227 *
228 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
229 * the low-level implementation of ev_get_gpe_event_info.
230 *
231 ******************************************************************************/
232
233struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
234 struct acpi_gpe_block_info
235 *gpe_block)
236{
237 u32 gpe_index;
238
239 /*
240 * Validate that the gpe_number is within the specified gpe_block.
241 * (Two steps)
242 */
243 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
244 return (NULL);
245 }
246
247 gpe_index = gpe_number - gpe_block->block_base_number;
248 if (gpe_index >= gpe_block->gpe_count) {
249 return (NULL);
250 }
251
252 return (&gpe_block->event_info[gpe_index]);
253}
254
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*******************************************************************************
257 *
258 * FUNCTION: acpi_ev_get_gpe_event_info
259 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800260 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * gpe_number - Raw GPE number
262 *
263 * RETURN: A GPE event_info struct. NULL if not a valid GPE
264 *
265 * DESCRIPTION: Returns the event_info struct associated with this GPE.
266 * Validates the gpe_block and the gpe_number
267 *
268 * Should be called only when the GPE lists are semaphore locked
269 * and not subject to change.
270 *
271 ******************************************************************************/
272
Len Brown4be44fc2005-08-05 00:44:28 -0400273struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
274 u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Len Brown4be44fc2005-08-05 00:44:28 -0400276 union acpi_operand_object *obj_desc;
Lin Ming0f849d22010-04-06 14:52:37 +0800277 struct acpi_gpe_event_info *gpe_info;
Bob Moore67a119f2008-06-10 13:42:13 +0800278 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Len Brown4be44fc2005-08-05 00:44:28 -0400280 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Bob Moore186c3072010-04-27 11:32:28 +0800282 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!gpe_device) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
287
288 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
Lin Ming0f849d22010-04-06 14:52:37 +0800289 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
290 acpi_gbl_gpe_fadt_blocks
291 [i]);
292 if (gpe_info) {
293 return (gpe_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 }
296
297 /* The gpe_number was not in the range of either FADT GPE block */
298
299 return (NULL);
300 }
301
302 /* A Non-NULL gpe_device means this is a GPE Block Device */
303
Len Brownfd350942007-05-09 23:34:35 -0400304 obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
305 gpe_device);
Len Brown4be44fc2005-08-05 00:44:28 -0400306 if (!obj_desc || !obj_desc->device.gpe_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return (NULL);
308 }
309
Lin Ming0f849d22010-04-06 14:52:37 +0800310 return (acpi_ev_low_get_gpe_info
311 (gpe_number, obj_desc->device.gpe_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/*******************************************************************************
315 *
316 * FUNCTION: acpi_ev_gpe_detect
317 *
318 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
319 * Can have multiple GPE blocks attached.
320 *
321 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
322 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800323 * DESCRIPTION: Detect if any GP events have occurred. This function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * executed at interrupt level.
325 *
326 ******************************************************************************/
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Len Brown4be44fc2005-08-05 00:44:28 -0400330 acpi_status status;
331 struct acpi_gpe_block_info *gpe_block;
Bob Moore08978312005-10-21 00:00:00 -0400332 struct acpi_gpe_register_info *gpe_register_info;
333 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
334 u8 enabled_status_byte;
335 u32 status_reg;
336 u32 enable_reg;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500337 acpi_cpu_flags flags;
Bob Moore67a119f2008-06-10 13:42:13 +0800338 u32 i;
339 u32 j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Bob Mooreb229cf92006-04-21 17:15:00 -0400341 ACPI_FUNCTION_NAME(ev_gpe_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Check for the case where there are no GPEs */
344
345 if (!gpe_xrupt_list) {
346 return (int_status);
347 }
348
Bob Moore967440e32006-06-23 17:04:00 -0400349 /*
350 * We need to obtain the GPE lock for both the data structs and registers
Bob Moore9f15fc62008-11-12 16:01:56 +0800351 * Note: Not necessary to obtain the hardware lock, since the GPE
352 * registers are owned by the gpe_lock.
Bob Moore967440e32006-06-23 17:04:00 -0400353 */
Len Brown4be44fc2005-08-05 00:44:28 -0400354 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Bob Moore4c90ece2006-06-08 16:29:00 -0400355
356 /* Examine all GPE blocks attached to this interrupt level */
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 gpe_block = gpe_xrupt_list->gpe_block_list_head;
359 while (gpe_block) {
360 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800361 * Read all of the 8-bit GPE status and enable registers in this GPE
362 * block, saving all of them. Find all currently active GP events.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
364 for (i = 0; i < gpe_block->register_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Get the next status/enable pair */
367
368 gpe_register_info = &gpe_block->register_info[i];
369
370 /* Read the Status Register */
371
Len Brown4be44fc2005-08-05 00:44:28 -0400372 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800373 acpi_hw_read(&status_reg,
374 &gpe_register_info->status_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400375 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto unlock_and_exit;
377 }
378
379 /* Read the Enable Register */
380
Len Brown4be44fc2005-08-05 00:44:28 -0400381 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800382 acpi_hw_read(&enable_reg,
383 &gpe_register_info->enable_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400384 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto unlock_and_exit;
386 }
387
Len Brown4be44fc2005-08-05 00:44:28 -0400388 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
389 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
390 gpe_register_info->base_gpe_number,
391 status_reg, enable_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Robert Moore44f6c012005-04-18 22:49:35 -0400393 /* Check if there is anything active at all in this register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 enabled_status_byte = (u8) (status_reg & enable_reg);
396 if (!enabled_status_byte) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* No active GPEs in this register, move on */
399
400 continue;
401 }
402
403 /* Now look at the individual GPEs in this byte register */
404
405 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* Examine one GPE bit */
408
Alexey Starikovskiy69874162007-02-02 19:48:19 +0300409 if (enabled_status_byte & (1 << j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Found an active GPE. Dispatch the event to a handler
412 * or method.
413 */
Len Brown4be44fc2005-08-05 00:44:28 -0400414 int_status |=
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800415 acpi_ev_gpe_dispatch(gpe_block->
416 node,
417 &gpe_block->
Bob Moore67a119f2008-06-10 13:42:13 +0800418 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
421 }
422
423 gpe_block = gpe_block->next;
424 }
425
Len Brown4be44fc2005-08-05 00:44:28 -0400426 unlock_and_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Len Brown4be44fc2005-08-05 00:44:28 -0400428 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return (int_status);
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/*******************************************************************************
433 *
434 * FUNCTION: acpi_ev_asynch_execute_gpe_method
435 *
436 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
437 *
438 * RETURN: None
439 *
Bob Moore41195322006-05-26 16:36:00 -0400440 * DESCRIPTION: Perform the actual execution of a GPE control method. This
441 * function is called from an invocation of acpi_os_execute and
442 * therefore does NOT execute at interrupt level - so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * the control method itself is not executed in the context of
444 * an interrupt handler.
445 *
446 ******************************************************************************/
447
Len Brown4be44fc2005-08-05 00:44:28 -0400448static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Lin Ming5a284cd2010-12-13 13:39:07 +0800450 struct acpi_gpe_event_info *gpe_event_info = context;
Len Brown4be44fc2005-08-05 00:44:28 -0400451 acpi_status status;
Lin Ming5a284cd2010-12-13 13:39:07 +0800452 struct acpi_gpe_event_info *local_gpe_event_info;
Bob Moore41195322006-05-26 16:36:00 -0400453 struct acpi_evaluate_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Bob Mooreb229cf92006-04-21 17:15:00 -0400455 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Lin Ming5a284cd2010-12-13 13:39:07 +0800457 /* Allocate a local GPE block */
458
459 local_gpe_event_info =
460 ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_event_info));
461 if (!local_gpe_event_info) {
462 ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "while handling a GPE"));
463 return_VOID;
464 }
465
Len Brown4be44fc2005-08-05 00:44:28 -0400466 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
467 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return_VOID;
469 }
470
471 /* Must revalidate the gpe_number/gpe_block */
472
Len Brown4be44fc2005-08-05 00:44:28 -0400473 if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
474 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return_VOID;
476 }
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800479 * Take a snapshot of the GPE info for this level - we copy the info to
480 * prevent a race condition with remove_handler/remove_block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Lin Ming5a284cd2010-12-13 13:39:07 +0800482 ACPI_MEMCPY(local_gpe_event_info, gpe_event_info,
Len Brown4be44fc2005-08-05 00:44:28 -0400483 sizeof(struct acpi_gpe_event_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
486 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return_VOID;
488 }
489
Lin Mingbba63a22010-12-13 13:39:17 +0800490 /* Do the correct dispatch - normal method or implicit notify */
491
492 switch (local_gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
493 case ACPI_GPE_DISPATCH_NOTIFY:
494
495 /*
496 * Implicit notify.
497 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
498 * NOTE: the request is queued for execution after this method
499 * completes. The notify handlers are NOT invoked synchronously
500 * from this thread -- because handlers may in turn run other
501 * control methods.
502 */
503 status =
504 acpi_ev_queue_notify_request(local_gpe_event_info->dispatch.
505 device_node,
506 ACPI_NOTIFY_DEVICE_WAKE);
507 break;
508
509 case ACPI_GPE_DISPATCH_METHOD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Bob Moore41195322006-05-26 16:36:00 -0400511 /* Allocate the evaluation information block */
512
513 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
514 if (!info) {
515 status = AE_NO_MEMORY;
516 } else {
517 /*
518 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
519 * control method that corresponds to this GPE
520 */
521 info->prefix_node =
Lin Ming5a284cd2010-12-13 13:39:07 +0800522 local_gpe_event_info->dispatch.method_node;
Bob Moore41195322006-05-26 16:36:00 -0400523 info->flags = ACPI_IGNORE_RETURN_VALUE;
524
525 status = acpi_ns_evaluate(info);
526 ACPI_FREE(info);
527 }
528
Len Brown4be44fc2005-08-05 00:44:28 -0400529 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500530 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore2e420052007-02-02 19:48:18 +0300531 "while evaluating GPE method [%4.4s]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500532 acpi_ut_get_node_name
Lin Ming5a284cd2010-12-13 13:39:07 +0800533 (local_gpe_event_info->dispatch.
Bob Moore4c90ece2006-06-08 16:29:00 -0400534 method_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Lin Mingbba63a22010-12-13 13:39:17 +0800536
537 break;
538
539 default:
540 return_VOID; /* Should never happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Lin Ming5a284cd2010-12-13 13:39:07 +0800542
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300543 /* Defer enabling of GPE until all notify handlers are done */
Lin Ming5a284cd2010-12-13 13:39:07 +0800544
545 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
546 acpi_ev_asynch_enable_gpe,
547 local_gpe_event_info);
548 if (ACPI_FAILURE(status)) {
549 ACPI_FREE(local_gpe_event_info);
550 }
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300551 return_VOID;
552}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Lin Mingbba63a22010-12-13 13:39:17 +0800554
Lin Ming5a284cd2010-12-13 13:39:07 +0800555/*******************************************************************************
556 *
557 * FUNCTION: acpi_ev_asynch_enable_gpe
558 *
559 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
560 * Callback from acpi_os_execute
561 *
562 * RETURN: None
563 *
564 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
Lin Mingbba63a22010-12-13 13:39:17 +0800565 * complete (i.e., finish execution of Notify)
Lin Ming5a284cd2010-12-13 13:39:07 +0800566 *
567 ******************************************************************************/
568
569static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300570{
571 struct acpi_gpe_event_info *gpe_event_info = context;
Lin Mingbba63a22010-12-13 13:39:17 +0800572
573 (void)acpi_ev_finish_gpe(gpe_event_info);
574
575 ACPI_FREE(gpe_event_info);
576 return;
577}
578
579
580/*******************************************************************************
581 *
582 * FUNCTION: acpi_ev_finish_gpe
583 *
584 * PARAMETERS: gpe_event_info - Info for this GPE
585 *
586 * RETURN: Status
587 *
588 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
589 * of a GPE method or a synchronous or asynchronous GPE handler.
590 *
591 ******************************************************************************/
592
593acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)
594{
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300595 acpi_status status;
Lin Ming5a284cd2010-12-13 13:39:07 +0800596
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300597 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400598 ACPI_GPE_LEVEL_TRIGGERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800600 * GPE is level-triggered, we clear the GPE status bit after
601 * handling the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300603 status = acpi_hw_clear_gpe(gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400604 if (ACPI_FAILURE(status)) {
Lin Mingbba63a22010-12-13 13:39:17 +0800605 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 }
608
Bob Moorede5668f2010-07-06 10:30:37 +0800609 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800610 * Enable this GPE, conditionally. This means that the GPE will
611 * only be physically enabled if the enable_for_run bit is set
612 * in the event_info.
Bob Moorede5668f2010-07-06 10:30:37 +0800613 */
Lin Ming3a378982010-12-13 13:36:15 +0800614 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
Lin Mingbba63a22010-12-13 13:39:17 +0800615 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Lin Mingbba63a22010-12-13 13:39:17 +0800618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*******************************************************************************
620 *
621 * FUNCTION: acpi_ev_gpe_dispatch
622 *
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800623 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
624 * gpe_event_info - Info for this GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 * gpe_number - Number relative to the parent GPE block
626 *
627 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
628 *
629 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
630 * or method (e.g. _Lxx/_Exx) handler.
631 *
632 * This function executes at interrupt level.
633 *
634 ******************************************************************************/
635
636u32
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800637acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
638 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Len Brown4be44fc2005-08-05 00:44:28 -0400640 acpi_status status;
Lin Mingbba63a22010-12-13 13:39:17 +0800641 u32 return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Bob Mooreb229cf92006-04-21 17:15:00 -0400643 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Lin Minga0fcdb22010-12-13 13:39:26 +0800645 /* Invoke global event handler if present */
646
647 acpi_gpe_count++;
648 if (acpi_gbl_global_event_handler) {
649 acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE, gpe_device,
650 gpe_number,
651 acpi_gbl_global_event_handler_context);
652 }
Bob Moorefdffb722007-02-02 19:48:19 +0300653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800655 * If edge-triggered, clear the GPE status bit now. Note that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * level-triggered events are cleared after the GPE is serviced.
657 */
Robert Moore44f6c012005-04-18 22:49:35 -0400658 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400659 ACPI_GPE_EDGE_TRIGGERED) {
660 status = acpi_hw_clear_gpe(gpe_event_info);
661 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500662 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800663 "Unable to clear GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500664 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400665 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 }
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800670 * Always disable the GPE so that it does not keep firing before
671 * any asynchronous activity completes (either from the execution
672 * of a GPE method or an asynchronous GPE handler.)
673 *
674 * If there is no handler or method to run, just disable the
675 * GPE and leave it disabled permanently to prevent further such
676 * pointless events from firing.
677 */
678 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
679 if (ACPI_FAILURE(status)) {
680 ACPI_EXCEPTION((AE_INFO, status,
681 "Unable to disable GPE%02X", gpe_number));
682 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
683 }
684
685 /*
686 * Dispatch the GPE to either an installed handler or the control
687 * method associated with this GPE (_Lxx or _Exx). If a handler
688 * exists, we invoke it and do not attempt to run the method.
689 * If there is neither a handler nor a method, leave the GPE
690 * disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
692 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
693 case ACPI_GPE_DISPATCH_HANDLER:
694
Lin Mingbba63a22010-12-13 13:39:17 +0800695 /* Invoke the installed handler (at interrupt level) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Lin Mingbba63a22010-12-13 13:39:17 +0800697 return_value =
698 gpe_event_info->dispatch.handler->address(gpe_device,
699 gpe_number,
700 gpe_event_info->
701 dispatch.handler->
702 context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Lin Mingbba63a22010-12-13 13:39:17 +0800704 /* If requested, clear (if level-triggered) and reenable the GPE */
705
706 if (return_value & ACPI_REENABLE_GPE) {
707 (void)acpi_ev_finish_gpe(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 break;
710
711 case ACPI_GPE_DISPATCH_METHOD:
Lin Mingbba63a22010-12-13 13:39:17 +0800712 case ACPI_GPE_DISPATCH_NOTIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /*
715 * Execute the method associated with the GPE
716 * NOTE: Level-triggered GPEs are cleared after the method completes.
717 */
Bob Moore958dd242006-05-12 17:12:00 -0400718 status = acpi_os_execute(OSL_GPE_HANDLER,
719 acpi_ev_asynch_execute_gpe_method,
720 gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400721 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500722 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800723 "Unable to queue handler for GPE[0x%2X] - event disabled",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500724 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 break;
727
728 default:
729
Lin Ming0f849d22010-04-06 14:52:37 +0800730 /*
731 * No handler or method to run!
732 * 03/2010: This case should no longer be possible. We will not allow
733 * a GPE to be enabled if it has no handler or method.
734 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500735 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800736 "No handler or method for GPE[0x%2X], disabling event",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500737 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 }
741
Bob Moore50eca3e2005-09-30 19:03:00 -0400742 return_UINT32(ACPI_INTERRUPT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}