blob: c25999546c8c58c897d03f54a563ab1e57aad1b2 [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 /*
118 * We will only allow a GPE to be enabled if it has either an
119 * associated method (_Lxx/_Exx) or a handler. Otherwise, the
120 * GPE will be immediately disabled by acpi_ev_gpe_dispatch the
121 * first time it fires.
122 */
123 if (!(gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)) {
124 return_ACPI_STATUS(AE_NO_HANDLER);
125 }
126
127 /* Clear the GPE (of stale events) */
128 status = acpi_hw_clear_gpe(gpe_event_info);
129 if (ACPI_FAILURE(status)) {
130 return_ACPI_STATUS(status);
131 }
132
133 /* Enable the requested GPE */
134 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
135
136 return_ACPI_STATUS(status);
137}
138
Lin Ming0f849d22010-04-06 14:52:37 +0800139
140/*******************************************************************************
141 *
Lin Ming3a378982010-12-13 13:36:15 +0800142 * FUNCTION: acpi_ev_add_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200143 *
144 * PARAMETERS: gpe_event_info - GPE to enable
145 *
146 * RETURN: Status
147 *
148 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
149 * hardware-enabled.
150 *
151 ******************************************************************************/
152
Lin Ming3a378982010-12-13 13:36:15 +0800153acpi_status acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200154{
155 acpi_status status = AE_OK;
156
Lin Ming3a378982010-12-13 13:36:15 +0800157 ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
158
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200159 if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
160 return_ACPI_STATUS(AE_LIMIT);
161 }
162
163 gpe_event_info->runtime_count++;
164 if (gpe_event_info->runtime_count == 1) {
165 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
166 if (ACPI_SUCCESS(status)) {
167 status = acpi_ev_enable_gpe(gpe_event_info);
168 }
169
170 if (ACPI_FAILURE(status)) {
171 gpe_event_info->runtime_count--;
172 }
173 }
174
175 return_ACPI_STATUS(status);
176}
177
178/*******************************************************************************
179 *
Lin Ming3a378982010-12-13 13:36:15 +0800180 * FUNCTION: acpi_ev_remove_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200181 *
182 * PARAMETERS: gpe_event_info - GPE to disable
183 *
184 * RETURN: Status
185 *
186 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
187 * removed, the GPE is hardware-disabled.
188 *
189 ******************************************************************************/
190
Lin Ming3a378982010-12-13 13:36:15 +0800191acpi_status acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200192{
193 acpi_status status = AE_OK;
194
Lin Ming3a378982010-12-13 13:36:15 +0800195 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
196
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200197 if (!gpe_event_info->runtime_count) {
198 return_ACPI_STATUS(AE_LIMIT);
199 }
200
201 gpe_event_info->runtime_count--;
202 if (!gpe_event_info->runtime_count) {
203 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
204 if (ACPI_SUCCESS(status)) {
205 status = acpi_hw_low_set_gpe(gpe_event_info,
206 ACPI_GPE_DISABLE);
207 }
208
209 if (ACPI_FAILURE(status)) {
210 gpe_event_info->runtime_count++;
211 }
212 }
213
214 return_ACPI_STATUS(status);
215}
216
217/*******************************************************************************
218 *
Lin Ming0f849d22010-04-06 14:52:37 +0800219 * FUNCTION: acpi_ev_low_get_gpe_info
220 *
221 * PARAMETERS: gpe_number - Raw GPE number
222 * gpe_block - A GPE info block
223 *
224 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
225 * is not within the specified GPE block)
226 *
227 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
228 * the low-level implementation of ev_get_gpe_event_info.
229 *
230 ******************************************************************************/
231
232struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
233 struct acpi_gpe_block_info
234 *gpe_block)
235{
236 u32 gpe_index;
237
238 /*
239 * Validate that the gpe_number is within the specified gpe_block.
240 * (Two steps)
241 */
242 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
243 return (NULL);
244 }
245
246 gpe_index = gpe_number - gpe_block->block_base_number;
247 if (gpe_index >= gpe_block->gpe_count) {
248 return (NULL);
249 }
250
251 return (&gpe_block->event_info[gpe_index]);
252}
253
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*******************************************************************************
256 *
257 * FUNCTION: acpi_ev_get_gpe_event_info
258 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800259 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * gpe_number - Raw GPE number
261 *
262 * RETURN: A GPE event_info struct. NULL if not a valid GPE
263 *
264 * DESCRIPTION: Returns the event_info struct associated with this GPE.
265 * Validates the gpe_block and the gpe_number
266 *
267 * Should be called only when the GPE lists are semaphore locked
268 * and not subject to change.
269 *
270 ******************************************************************************/
271
Len Brown4be44fc2005-08-05 00:44:28 -0400272struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
273 u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Len Brown4be44fc2005-08-05 00:44:28 -0400275 union acpi_operand_object *obj_desc;
Lin Ming0f849d22010-04-06 14:52:37 +0800276 struct acpi_gpe_event_info *gpe_info;
Bob Moore67a119f2008-06-10 13:42:13 +0800277 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Len Brown4be44fc2005-08-05 00:44:28 -0400279 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Bob Moore186c3072010-04-27 11:32:28 +0800281 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (!gpe_device) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
286
287 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
Lin Ming0f849d22010-04-06 14:52:37 +0800288 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
289 acpi_gbl_gpe_fadt_blocks
290 [i]);
291 if (gpe_info) {
292 return (gpe_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 }
295
296 /* The gpe_number was not in the range of either FADT GPE block */
297
298 return (NULL);
299 }
300
301 /* A Non-NULL gpe_device means this is a GPE Block Device */
302
Len Brownfd350942007-05-09 23:34:35 -0400303 obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
304 gpe_device);
Len Brown4be44fc2005-08-05 00:44:28 -0400305 if (!obj_desc || !obj_desc->device.gpe_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return (NULL);
307 }
308
Lin Ming0f849d22010-04-06 14:52:37 +0800309 return (acpi_ev_low_get_gpe_info
310 (gpe_number, obj_desc->device.gpe_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*******************************************************************************
314 *
315 * FUNCTION: acpi_ev_gpe_detect
316 *
317 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
318 * Can have multiple GPE blocks attached.
319 *
320 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
321 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800322 * DESCRIPTION: Detect if any GP events have occurred. This function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * executed at interrupt level.
324 *
325 ******************************************************************************/
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Len Brown4be44fc2005-08-05 00:44:28 -0400329 acpi_status status;
330 struct acpi_gpe_block_info *gpe_block;
Bob Moore08978312005-10-21 00:00:00 -0400331 struct acpi_gpe_register_info *gpe_register_info;
332 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
333 u8 enabled_status_byte;
334 u32 status_reg;
335 u32 enable_reg;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500336 acpi_cpu_flags flags;
Bob Moore67a119f2008-06-10 13:42:13 +0800337 u32 i;
338 u32 j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Bob Mooreb229cf92006-04-21 17:15:00 -0400340 ACPI_FUNCTION_NAME(ev_gpe_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Check for the case where there are no GPEs */
343
344 if (!gpe_xrupt_list) {
345 return (int_status);
346 }
347
Bob Moore967440e2006-06-23 17:04:00 -0400348 /*
349 * We need to obtain the GPE lock for both the data structs and registers
Bob Moore9f15fc62008-11-12 16:01:56 +0800350 * Note: Not necessary to obtain the hardware lock, since the GPE
351 * registers are owned by the gpe_lock.
Bob Moore967440e2006-06-23 17:04:00 -0400352 */
Len Brown4be44fc2005-08-05 00:44:28 -0400353 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Bob Moore4c90ece2006-06-08 16:29:00 -0400354
355 /* Examine all GPE blocks attached to this interrupt level */
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 gpe_block = gpe_xrupt_list->gpe_block_list_head;
358 while (gpe_block) {
359 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800360 * Read all of the 8-bit GPE status and enable registers in this GPE
361 * block, saving all of them. Find all currently active GP events.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363 for (i = 0; i < gpe_block->register_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* Get the next status/enable pair */
366
367 gpe_register_info = &gpe_block->register_info[i];
368
369 /* Read the Status Register */
370
Len Brown4be44fc2005-08-05 00:44:28 -0400371 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800372 acpi_hw_read(&status_reg,
373 &gpe_register_info->status_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400374 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto unlock_and_exit;
376 }
377
378 /* Read the Enable Register */
379
Len Brown4be44fc2005-08-05 00:44:28 -0400380 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800381 acpi_hw_read(&enable_reg,
382 &gpe_register_info->enable_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400383 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto unlock_and_exit;
385 }
386
Len Brown4be44fc2005-08-05 00:44:28 -0400387 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
388 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
389 gpe_register_info->base_gpe_number,
390 status_reg, enable_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Robert Moore44f6c012005-04-18 22:49:35 -0400392 /* Check if there is anything active at all in this register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 enabled_status_byte = (u8) (status_reg & enable_reg);
395 if (!enabled_status_byte) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* No active GPEs in this register, move on */
398
399 continue;
400 }
401
402 /* Now look at the individual GPEs in this byte register */
403
404 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* Examine one GPE bit */
407
Alexey Starikovskiy69874162007-02-02 19:48:19 +0300408 if (enabled_status_byte & (1 << j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Found an active GPE. Dispatch the event to a handler
411 * or method.
412 */
Len Brown4be44fc2005-08-05 00:44:28 -0400413 int_status |=
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800414 acpi_ev_gpe_dispatch(gpe_block->
415 node,
416 &gpe_block->
Bob Moore67a119f2008-06-10 13:42:13 +0800417 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 }
420 }
421
422 gpe_block = gpe_block->next;
423 }
424
Len Brown4be44fc2005-08-05 00:44:28 -0400425 unlock_and_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Len Brown4be44fc2005-08-05 00:44:28 -0400427 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return (int_status);
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*******************************************************************************
432 *
433 * FUNCTION: acpi_ev_asynch_execute_gpe_method
434 *
435 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
436 *
437 * RETURN: None
438 *
Bob Moore41195322006-05-26 16:36:00 -0400439 * DESCRIPTION: Perform the actual execution of a GPE control method. This
440 * function is called from an invocation of acpi_os_execute and
441 * therefore does NOT execute at interrupt level - so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * the control method itself is not executed in the context of
443 * an interrupt handler.
444 *
445 ******************************************************************************/
446
Len Brown4be44fc2005-08-05 00:44:28 -0400447static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Lin Ming5a284cd2010-12-13 13:39:07 +0800449 struct acpi_gpe_event_info *gpe_event_info = context;
Len Brown4be44fc2005-08-05 00:44:28 -0400450 acpi_status status;
Lin Ming5a284cd2010-12-13 13:39:07 +0800451 struct acpi_gpe_event_info *local_gpe_event_info;
Bob Moore41195322006-05-26 16:36:00 -0400452 struct acpi_evaluate_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Bob Mooreb229cf92006-04-21 17:15:00 -0400454 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Lin Ming5a284cd2010-12-13 13:39:07 +0800456 /* Allocate a local GPE block */
457
458 local_gpe_event_info =
459 ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_event_info));
460 if (!local_gpe_event_info) {
461 ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "while handling a GPE"));
462 return_VOID;
463 }
464
Len Brown4be44fc2005-08-05 00:44:28 -0400465 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
466 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return_VOID;
468 }
469
470 /* Must revalidate the gpe_number/gpe_block */
471
Len Brown4be44fc2005-08-05 00:44:28 -0400472 if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
473 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return_VOID;
475 }
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800478 * Take a snapshot of the GPE info for this level - we copy the info to
479 * prevent a race condition with remove_handler/remove_block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
Lin Ming5a284cd2010-12-13 13:39:07 +0800481 ACPI_MEMCPY(local_gpe_event_info, gpe_event_info,
Len Brown4be44fc2005-08-05 00:44:28 -0400482 sizeof(struct acpi_gpe_event_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Len Brown4be44fc2005-08-05 00:44:28 -0400484 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
485 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return_VOID;
487 }
488
489 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800490 * Must check for control method type dispatch one more time to avoid a
491 * race with ev_gpe_install_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Lin Ming5a284cd2010-12-13 13:39:07 +0800493 if ((local_gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400494 ACPI_GPE_DISPATCH_METHOD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Bob Moore41195322006-05-26 16:36:00 -0400496 /* Allocate the evaluation information block */
497
498 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
499 if (!info) {
500 status = AE_NO_MEMORY;
501 } else {
502 /*
503 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
504 * control method that corresponds to this GPE
505 */
506 info->prefix_node =
Lin Ming5a284cd2010-12-13 13:39:07 +0800507 local_gpe_event_info->dispatch.method_node;
Bob Moore41195322006-05-26 16:36:00 -0400508 info->flags = ACPI_IGNORE_RETURN_VALUE;
509
510 status = acpi_ns_evaluate(info);
511 ACPI_FREE(info);
512 }
513
Len Brown4be44fc2005-08-05 00:44:28 -0400514 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500515 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore2e420052007-02-02 19:48:18 +0300516 "while evaluating GPE method [%4.4s]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500517 acpi_ut_get_node_name
Lin Ming5a284cd2010-12-13 13:39:07 +0800518 (local_gpe_event_info->dispatch.
Bob Moore4c90ece2006-06-08 16:29:00 -0400519 method_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 }
Lin Ming5a284cd2010-12-13 13:39:07 +0800522
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300523 /* Defer enabling of GPE until all notify handlers are done */
Lin Ming5a284cd2010-12-13 13:39:07 +0800524
525 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
526 acpi_ev_asynch_enable_gpe,
527 local_gpe_event_info);
528 if (ACPI_FAILURE(status)) {
529 ACPI_FREE(local_gpe_event_info);
530 }
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300531 return_VOID;
532}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Lin Ming5a284cd2010-12-13 13:39:07 +0800534/*******************************************************************************
535 *
536 * FUNCTION: acpi_ev_asynch_enable_gpe
537 *
538 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
539 * Callback from acpi_os_execute
540 *
541 * RETURN: None
542 *
543 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
544 * complete.
545 *
546 ******************************************************************************/
547
548static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300549{
550 struct acpi_gpe_event_info *gpe_event_info = context;
551 acpi_status status;
Lin Ming5a284cd2010-12-13 13:39:07 +0800552
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300553 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400554 ACPI_GPE_LEVEL_TRIGGERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800556 * GPE is level-triggered, we clear the GPE status bit after handling
557 * the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300559 status = acpi_hw_clear_gpe(gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400560 if (ACPI_FAILURE(status)) {
Lin Ming5a284cd2010-12-13 13:39:07 +0800561 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 }
564
Bob Moorede5668f2010-07-06 10:30:37 +0800565 /*
566 * Enable this GPE, conditionally. This means that the GPE will only be
567 * physically enabled if the enable_for_run bit is set in the event_info
568 */
Lin Ming3a378982010-12-13 13:36:15 +0800569 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
Bob Moorede5668f2010-07-06 10:30:37 +0800570
Lin Ming5a284cd2010-12-13 13:39:07 +0800571exit:
572 ACPI_FREE(gpe_event_info);
573 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*******************************************************************************
577 *
578 * FUNCTION: acpi_ev_gpe_dispatch
579 *
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800580 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
581 * gpe_event_info - Info for this GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * gpe_number - Number relative to the parent GPE block
583 *
584 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
585 *
586 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
587 * or method (e.g. _Lxx/_Exx) handler.
588 *
589 * This function executes at interrupt level.
590 *
591 ******************************************************************************/
592
593u32
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800594acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
595 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Len Brown4be44fc2005-08-05 00:44:28 -0400597 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Bob Mooreb229cf92006-04-21 17:15:00 -0400599 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Len Brown5229e872008-02-06 01:26:55 -0500601 acpi_os_gpe_count(gpe_number);
Bob Moorefdffb722007-02-02 19:48:19 +0300602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800604 * If edge-triggered, clear the GPE status bit now. Note that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * level-triggered events are cleared after the GPE is serviced.
606 */
Robert Moore44f6c012005-04-18 22:49:35 -0400607 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400608 ACPI_GPE_EDGE_TRIGGERED) {
609 status = acpi_hw_clear_gpe(gpe_event_info);
610 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500611 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800612 "Unable to clear GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500613 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400614 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 }
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
Bob Moorec5a71562007-02-02 19:48:19 +0300619 * Dispatch the GPE to either an installed handler, or the control method
620 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
621 * it and do not attempt to run the method. If there is neither a handler
622 * nor a method, we disable this GPE to prevent further such pointless
623 * events from firing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
625 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
626 case ACPI_GPE_DISPATCH_HANDLER:
627
628 /*
629 * Invoke the installed handler (at interrupt level)
Bob Moore9f15fc62008-11-12 16:01:56 +0800630 * Ignore return status for now.
631 * TBD: leave GPE disabled on error?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800633 (void)gpe_event_info->dispatch.handler->address(gpe_device,
634 gpe_number,
635 gpe_event_info->
Len Brown4be44fc2005-08-05 00:44:28 -0400636 dispatch.
637 handler->
638 context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* It is now safe to clear level-triggered events. */
641
Robert Moore44f6c012005-04-18 22:49:35 -0400642 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400643 ACPI_GPE_LEVEL_TRIGGERED) {
644 status = acpi_hw_clear_gpe(gpe_event_info);
645 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500646 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800647 "Unable to clear GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500648 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400649 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 }
652 break;
653
654 case ACPI_GPE_DISPATCH_METHOD:
655
656 /*
Bob Moorec5a71562007-02-02 19:48:19 +0300657 * Disable the GPE, so it doesn't keep firing before the method has a
658 * chance to run (it runs asynchronously with interrupts enabled).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
Rafael J. Wysockifd247442010-06-08 10:49:08 +0200660 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
Len Brown4be44fc2005-08-05 00:44:28 -0400661 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 disable 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 /*
669 * Execute the method associated with the GPE
670 * NOTE: Level-triggered GPEs are cleared after the method completes.
671 */
Bob Moore958dd242006-05-12 17:12:00 -0400672 status = acpi_os_execute(OSL_GPE_HANDLER,
673 acpi_ev_asynch_execute_gpe_method,
674 gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400675 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500676 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800677 "Unable to queue handler for GPE[0x%2X] - event disabled",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500678 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 break;
681
682 default:
683
Lin Ming0f849d22010-04-06 14:52:37 +0800684 /*
685 * No handler or method to run!
686 * 03/2010: This case should no longer be possible. We will not allow
687 * a GPE to be enabled if it has no handler or method.
688 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500689 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800690 "No handler or method for GPE[0x%2X], disabling event",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500691 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /*
Lin Ming0f849d22010-04-06 14:52:37 +0800694 * Disable the GPE. The GPE will remain disabled a handler
695 * is installed or ACPICA is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Rafael J. Wysockifd247442010-06-08 10:49:08 +0200697 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
Len Brown4be44fc2005-08-05 00:44:28 -0400698 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500699 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800700 "Unable to disable GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500701 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400702 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 break;
705 }
706
Bob Moore50eca3e2005-09-30 19:03:00 -0400707 return_UINT32(ACPI_INTERRUPT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}