blob: f345ced3647731f4365f4e8e657caec8aaac84d8 [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/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, 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
55/*******************************************************************************
56 *
57 * FUNCTION: acpi_ev_set_gpe_type
58 *
59 * PARAMETERS: gpe_event_info - GPE to set
60 * Type - New type
61 *
62 * RETURN: Status
63 *
64 * DESCRIPTION: Sets the new type for the GPE (wake, run, or wake/run)
65 *
66 ******************************************************************************/
67
68acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040069acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Len Brown4be44fc2005-08-05 00:44:28 -040071 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Bob Mooreb229cf92006-04-21 17:15:00 -040073 ACPI_FUNCTION_TRACE(ev_set_gpe_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 /* Validate type and update register enable masks */
76
77 switch (type) {
78 case ACPI_GPE_TYPE_WAKE:
79 case ACPI_GPE_TYPE_RUNTIME:
80 case ACPI_GPE_TYPE_WAKE_RUN:
81 break;
82
83 default:
Len Brown4be44fc2005-08-05 00:44:28 -040084 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 /* Disable the GPE if currently enabled */
88
Len Brown4be44fc2005-08-05 00:44:28 -040089 status = acpi_ev_disable_gpe(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /* Type was validated above */
92
Len Brown4be44fc2005-08-05 00:44:28 -040093 gpe_event_info->flags &= ~ACPI_GPE_TYPE_MASK; /* Clear type bits */
94 gpe_event_info->flags |= type; /* Insert type */
95 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*******************************************************************************
99 *
100 * FUNCTION: acpi_ev_update_gpe_enable_masks
101 *
102 * PARAMETERS: gpe_event_info - GPE to update
103 * Type - What to do: ACPI_GPE_DISABLE or
104 * ACPI_GPE_ENABLE
105 *
106 * RETURN: Status
107 *
108 * DESCRIPTION: Updates GPE register enable masks based on the GPE type
109 *
110 ******************************************************************************/
111
112acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400113acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
114 u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Len Brown4be44fc2005-08-05 00:44:28 -0400116 struct acpi_gpe_register_info *gpe_register_info;
117 u8 register_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Bob Mooreb229cf92006-04-21 17:15:00 -0400119 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_masks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 gpe_register_info = gpe_event_info->register_info;
122 if (!gpe_register_info) {
Len Brown4be44fc2005-08-05 00:44:28 -0400123 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Alexey Starikovskiy69874162007-02-02 19:48:19 +0300125 register_bit = (u8)
126 (1 <<
127 (gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Bob Moore9f15fc62008-11-12 16:01:56 +0800129 /* 1) Disable case. Simply clear all enable bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (type == ACPI_GPE_DISABLE) {
Len Brown4be44fc2005-08-05 00:44:28 -0400132 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
133 register_bit);
134 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
135 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137
Bob Moore9f15fc62008-11-12 16:01:56 +0800138 /* 2) Enable case. Set/Clear the appropriate enable bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
141 case ACPI_GPE_TYPE_WAKE:
Len Brown4be44fc2005-08-05 00:44:28 -0400142 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
143 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 break;
145
146 case ACPI_GPE_TYPE_RUNTIME:
Len Brown4be44fc2005-08-05 00:44:28 -0400147 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
148 register_bit);
149 ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
151
152 case ACPI_GPE_TYPE_WAKE_RUN:
Len Brown4be44fc2005-08-05 00:44:28 -0400153 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
154 ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
156
157 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400158 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
Len Brown4be44fc2005-08-05 00:44:28 -0400161 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*******************************************************************************
165 *
166 * FUNCTION: acpi_ev_enable_gpe
167 *
168 * PARAMETERS: gpe_event_info - GPE to enable
169 * write_to_hardware - Enable now, or just mark data structs
170 * (WAKE GPEs should be deferred)
171 *
172 * RETURN: Status
173 *
174 * DESCRIPTION: Enable a GPE based on the GPE type
175 *
176 ******************************************************************************/
177
178acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400179acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
180 u8 write_to_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Len Brown4be44fc2005-08-05 00:44:28 -0400182 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Bob Mooreb229cf92006-04-21 17:15:00 -0400184 ACPI_FUNCTION_TRACE(ev_enable_gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* Make sure HW enable masks are updated */
187
Len Brown4be44fc2005-08-05 00:44:28 -0400188 status =
189 acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_ENABLE);
190 if (ACPI_FAILURE(status)) {
191 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
194 /* Mark wake-enabled or HW enable, or both */
195
196 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
197 case ACPI_GPE_TYPE_WAKE:
198
Len Brown4be44fc2005-08-05 00:44:28 -0400199 ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201
202 case ACPI_GPE_TYPE_WAKE_RUN:
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204 ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /*lint -fallthrough */
207
208 case ACPI_GPE_TYPE_RUNTIME:
209
Len Brown4be44fc2005-08-05 00:44:28 -0400210 ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (write_to_hardware) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* Clear the GPE (of stale events), then enable it */
215
Len Brown4be44fc2005-08-05 00:44:28 -0400216 status = acpi_hw_clear_gpe(gpe_event_info);
217 if (ACPI_FAILURE(status)) {
218 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
221 /* Enable the requested runtime GPE */
222
Len Brown4be44fc2005-08-05 00:44:28 -0400223 status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 break;
226
227 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400228 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
Len Brown4be44fc2005-08-05 00:44:28 -0400231 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*******************************************************************************
235 *
236 * FUNCTION: acpi_ev_disable_gpe
237 *
238 * PARAMETERS: gpe_event_info - GPE to disable
239 *
240 * RETURN: Status
241 *
242 * DESCRIPTION: Disable a GPE based on the GPE type
243 *
244 ******************************************************************************/
245
Len Brown4be44fc2005-08-05 00:44:28 -0400246acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Len Brown4be44fc2005-08-05 00:44:28 -0400248 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Bob Mooreb229cf92006-04-21 17:15:00 -0400250 ACPI_FUNCTION_TRACE(ev_disable_gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Make sure HW enable masks are updated */
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254 status =
255 acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_DISABLE);
256 if (ACPI_FAILURE(status)) {
257 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Bob Mooree38e8a02008-06-13 08:28:55 +0800260 /* Clear the appropriate enabled flags for this GPE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
263 case ACPI_GPE_TYPE_WAKE:
Len Brown4be44fc2005-08-05 00:44:28 -0400264 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 break;
266
267 case ACPI_GPE_TYPE_WAKE_RUN:
Len Brown4be44fc2005-08-05 00:44:28 -0400268 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Zhang Ruia7f9b1f2007-11-20 13:38:59 -0500270 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 case ACPI_GPE_TYPE_RUNTIME:
273
274 /* Disable the requested runtime GPE */
275
Len Brown4be44fc2005-08-05 00:44:28 -0400276 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
Bob Mooree38e8a02008-06-13 08:28:55 +0800277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 default:
Bob Mooree38e8a02008-06-13 08:28:55 +0800280 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Bob Mooree38e8a02008-06-13 08:28:55 +0800283 /*
284 * Even if we don't know the GPE type, make sure that we always
285 * disable it. low_disable_gpe will just clear the enable bit for this
286 * GPE and write it. It will not write out the current GPE enable mask,
287 * since this may inadvertently enable GPEs too early, if a rogue GPE has
288 * come in during ACPICA initialization - possibly as a result of AML or
289 * other code that has enabled the GPE.
290 */
291 status = acpi_hw_low_disable_gpe(gpe_event_info);
292 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*******************************************************************************
296 *
297 * FUNCTION: acpi_ev_get_gpe_event_info
298 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800299 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * gpe_number - Raw GPE number
301 *
302 * RETURN: A GPE event_info struct. NULL if not a valid GPE
303 *
304 * DESCRIPTION: Returns the event_info struct associated with this GPE.
305 * Validates the gpe_block and the gpe_number
306 *
307 * Should be called only when the GPE lists are semaphore locked
308 * and not subject to change.
309 *
310 ******************************************************************************/
311
Len Brown4be44fc2005-08-05 00:44:28 -0400312struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
313 u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Len Brown4be44fc2005-08-05 00:44:28 -0400315 union acpi_operand_object *obj_desc;
316 struct acpi_gpe_block_info *gpe_block;
Bob Moore67a119f2008-06-10 13:42:13 +0800317 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Len Brown4be44fc2005-08-05 00:44:28 -0400319 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* A NULL gpe_block means use the FADT-defined GPE block(s) */
322
323 if (!gpe_device) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
326
327 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
328 gpe_block = acpi_gbl_gpe_fadt_blocks[i];
329 if (gpe_block) {
Len Brown4be44fc2005-08-05 00:44:28 -0400330 if ((gpe_number >= gpe_block->block_base_number)
331 && (gpe_number <
332 gpe_block->block_base_number +
333 (gpe_block->register_count * 8))) {
334 return (&gpe_block->
335 event_info[gpe_number -
336 gpe_block->
337 block_base_number]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 }
340 }
341
342 /* The gpe_number was not in the range of either FADT GPE block */
343
344 return (NULL);
345 }
346
347 /* A Non-NULL gpe_device means this is a GPE Block Device */
348
Len Brownfd350942007-05-09 23:34:35 -0400349 obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
350 gpe_device);
Len Brown4be44fc2005-08-05 00:44:28 -0400351 if (!obj_desc || !obj_desc->device.gpe_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return (NULL);
353 }
354
355 gpe_block = obj_desc->device.gpe_block;
356
357 if ((gpe_number >= gpe_block->block_base_number) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400358 (gpe_number <
359 gpe_block->block_base_number + (gpe_block->register_count * 8))) {
360 return (&gpe_block->
361 event_info[gpe_number - gpe_block->block_base_number]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 return (NULL);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*******************************************************************************
368 *
369 * FUNCTION: acpi_ev_gpe_detect
370 *
371 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
372 * Can have multiple GPE blocks attached.
373 *
374 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
375 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800376 * DESCRIPTION: Detect if any GP events have occurred. This function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * executed at interrupt level.
378 *
379 ******************************************************************************/
380
Len Brown4be44fc2005-08-05 00:44:28 -0400381u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Len Brown4be44fc2005-08-05 00:44:28 -0400383 acpi_status status;
384 struct acpi_gpe_block_info *gpe_block;
Bob Moore08978312005-10-21 00:00:00 -0400385 struct acpi_gpe_register_info *gpe_register_info;
386 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
387 u8 enabled_status_byte;
388 u32 status_reg;
389 u32 enable_reg;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500390 acpi_cpu_flags flags;
Bob Moore67a119f2008-06-10 13:42:13 +0800391 u32 i;
392 u32 j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Bob Mooreb229cf92006-04-21 17:15:00 -0400394 ACPI_FUNCTION_NAME(ev_gpe_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* Check for the case where there are no GPEs */
397
398 if (!gpe_xrupt_list) {
399 return (int_status);
400 }
401
Bob Moore967440e32006-06-23 17:04:00 -0400402 /*
403 * We need to obtain the GPE lock for both the data structs and registers
Bob Moore9f15fc62008-11-12 16:01:56 +0800404 * Note: Not necessary to obtain the hardware lock, since the GPE
405 * registers are owned by the gpe_lock.
Bob Moore967440e32006-06-23 17:04:00 -0400406 */
Len Brown4be44fc2005-08-05 00:44:28 -0400407 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Bob Moore4c90ece2006-06-08 16:29:00 -0400408
409 /* Examine all GPE blocks attached to this interrupt level */
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 gpe_block = gpe_xrupt_list->gpe_block_list_head;
412 while (gpe_block) {
413 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800414 * Read all of the 8-bit GPE status and enable registers in this GPE
415 * block, saving all of them. Find all currently active GP events.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
417 for (i = 0; i < gpe_block->register_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Get the next status/enable pair */
420
421 gpe_register_info = &gpe_block->register_info[i];
422
423 /* Read the Status Register */
424
Len Brown4be44fc2005-08-05 00:44:28 -0400425 status =
Bob Mooreecfbbc72008-12-31 02:55:32 +0800426 acpi_read(&status_reg,
427 &gpe_register_info->status_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400428 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto unlock_and_exit;
430 }
431
432 /* Read the Enable Register */
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 status =
Bob Mooreecfbbc72008-12-31 02:55:32 +0800435 acpi_read(&enable_reg,
436 &gpe_register_info->enable_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400437 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto unlock_and_exit;
439 }
440
Len Brown4be44fc2005-08-05 00:44:28 -0400441 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
442 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
443 gpe_register_info->base_gpe_number,
444 status_reg, enable_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Robert Moore44f6c012005-04-18 22:49:35 -0400446 /* Check if there is anything active at all in this register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 enabled_status_byte = (u8) (status_reg & enable_reg);
449 if (!enabled_status_byte) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* No active GPEs in this register, move on */
452
453 continue;
454 }
455
456 /* Now look at the individual GPEs in this byte register */
457
458 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Examine one GPE bit */
461
Alexey Starikovskiy69874162007-02-02 19:48:19 +0300462 if (enabled_status_byte & (1 << j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Found an active GPE. Dispatch the event to a handler
465 * or method.
466 */
Len Brown4be44fc2005-08-05 00:44:28 -0400467 int_status |=
468 acpi_ev_gpe_dispatch(&gpe_block->
Bob Moore67a119f2008-06-10 13:42:13 +0800469 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 }
472 }
473
474 gpe_block = gpe_block->next;
475 }
476
Len Brown4be44fc2005-08-05 00:44:28 -0400477 unlock_and_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Len Brown4be44fc2005-08-05 00:44:28 -0400479 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return (int_status);
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*******************************************************************************
484 *
485 * FUNCTION: acpi_ev_asynch_execute_gpe_method
486 *
487 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
488 *
489 * RETURN: None
490 *
Bob Moore41195322006-05-26 16:36:00 -0400491 * DESCRIPTION: Perform the actual execution of a GPE control method. This
492 * function is called from an invocation of acpi_os_execute and
493 * therefore does NOT execute at interrupt level - so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * the control method itself is not executed in the context of
495 * an interrupt handler.
496 *
497 ******************************************************************************/
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300498static void acpi_ev_asynch_enable_gpe(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Len Brown4be44fc2005-08-05 00:44:28 -0400500static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Len Brown4be44fc2005-08-05 00:44:28 -0400502 struct acpi_gpe_event_info *gpe_event_info = (void *)context;
Len Brown4be44fc2005-08-05 00:44:28 -0400503 acpi_status status;
504 struct acpi_gpe_event_info local_gpe_event_info;
Bob Moore41195322006-05-26 16:36:00 -0400505 struct acpi_evaluate_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Bob Mooreb229cf92006-04-21 17:15:00 -0400507 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Len Brown4be44fc2005-08-05 00:44:28 -0400509 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
510 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return_VOID;
512 }
513
514 /* Must revalidate the gpe_number/gpe_block */
515
Len Brown4be44fc2005-08-05 00:44:28 -0400516 if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
517 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return_VOID;
519 }
520
521 /* Set the GPE flags for return to enabled state */
522
Len Brown4be44fc2005-08-05 00:44:28 -0400523 (void)acpi_ev_enable_gpe(gpe_event_info, FALSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800526 * Take a snapshot of the GPE info for this level - we copy the info to
527 * prevent a race condition with remove_handler/remove_block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Len Brown4be44fc2005-08-05 00:44:28 -0400529 ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
530 sizeof(struct acpi_gpe_event_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Len Brown4be44fc2005-08-05 00:44:28 -0400532 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
533 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return_VOID;
535 }
536
537 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800538 * Must check for control method type dispatch one more time to avoid a
539 * race with ev_gpe_install_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Robert Moore44f6c012005-04-18 22:49:35 -0400541 if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400542 ACPI_GPE_DISPATCH_METHOD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Bob Moore41195322006-05-26 16:36:00 -0400544 /* Allocate the evaluation information block */
545
546 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
547 if (!info) {
548 status = AE_NO_MEMORY;
549 } else {
550 /*
551 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
552 * control method that corresponds to this GPE
553 */
554 info->prefix_node =
555 local_gpe_event_info.dispatch.method_node;
Bob Moore41195322006-05-26 16:36:00 -0400556 info->flags = ACPI_IGNORE_RETURN_VALUE;
557
558 status = acpi_ns_evaluate(info);
559 ACPI_FREE(info);
560 }
561
Len Brown4be44fc2005-08-05 00:44:28 -0400562 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500563 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore2e420052007-02-02 19:48:18 +0300564 "while evaluating GPE method [%4.4s]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500565 acpi_ut_get_node_name
566 (local_gpe_event_info.dispatch.
Bob Moore4c90ece2006-06-08 16:29:00 -0400567 method_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 }
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300570 /* Defer enabling of GPE until all notify handlers are done */
571 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
572 gpe_event_info);
573 return_VOID;
574}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300576static void acpi_ev_asynch_enable_gpe(void *context)
577{
578 struct acpi_gpe_event_info *gpe_event_info = context;
579 acpi_status status;
580 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400581 ACPI_GPE_LEVEL_TRIGGERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800583 * GPE is level-triggered, we clear the GPE status bit after handling
584 * the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300586 status = acpi_hw_clear_gpe(gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400587 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return_VOID;
589 }
590 }
591
592 /* Enable this GPE */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300593 (void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return_VOID;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/*******************************************************************************
598 *
599 * FUNCTION: acpi_ev_gpe_dispatch
600 *
Robert Moore44f6c012005-04-18 22:49:35 -0400601 * PARAMETERS: gpe_event_info - Info for this GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * gpe_number - Number relative to the parent GPE block
603 *
604 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
605 *
606 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
607 * or method (e.g. _Lxx/_Exx) handler.
608 *
609 * This function executes at interrupt level.
610 *
611 ******************************************************************************/
612
613u32
Len Brown4be44fc2005-08-05 00:44:28 -0400614acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Len Brown4be44fc2005-08-05 00:44:28 -0400616 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Bob Mooreb229cf92006-04-21 17:15:00 -0400618 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Len Brown5229e872008-02-06 01:26:55 -0500620 acpi_os_gpe_count(gpe_number);
Bob Moorefdffb722007-02-02 19:48:19 +0300621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800623 * If edge-triggered, clear the GPE status bit now. Note that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * level-triggered events are cleared after the GPE is serviced.
625 */
Robert Moore44f6c012005-04-18 22:49:35 -0400626 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400627 ACPI_GPE_EDGE_TRIGGERED) {
628 status = acpi_hw_clear_gpe(gpe_event_info);
629 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500630 ACPI_EXCEPTION((AE_INFO, status,
631 "Unable to clear GPE[%2X]",
632 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400633 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
Bob Moorec5a71562007-02-02 19:48:19 +0300638 * Dispatch the GPE to either an installed handler, or the control method
639 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
640 * it and do not attempt to run the method. If there is neither a handler
641 * nor a method, we disable this GPE to prevent further such pointless
642 * events from firing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
645 case ACPI_GPE_DISPATCH_HANDLER:
646
647 /*
648 * Invoke the installed handler (at interrupt level)
Bob Moore9f15fc62008-11-12 16:01:56 +0800649 * Ignore return status for now.
650 * TBD: leave GPE disabled on error?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 */
Len Brown4be44fc2005-08-05 00:44:28 -0400652 (void)gpe_event_info->dispatch.handler->address(gpe_event_info->
653 dispatch.
654 handler->
655 context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* It is now safe to clear level-triggered events. */
658
Robert Moore44f6c012005-04-18 22:49:35 -0400659 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400660 ACPI_GPE_LEVEL_TRIGGERED) {
661 status = acpi_hw_clear_gpe(gpe_event_info);
662 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500663 ACPI_EXCEPTION((AE_INFO, status,
664 "Unable to clear GPE[%2X]",
665 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400666 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 }
669 break;
670
671 case ACPI_GPE_DISPATCH_METHOD:
672
673 /*
Bob Moorec5a71562007-02-02 19:48:19 +0300674 * Disable the GPE, so it doesn't keep firing before the method has a
675 * chance to run (it runs asynchronously with interrupts enabled).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Len Brown4be44fc2005-08-05 00:44:28 -0400677 status = acpi_ev_disable_gpe(gpe_event_info);
678 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500679 ACPI_EXCEPTION((AE_INFO, status,
680 "Unable to disable GPE[%2X]",
681 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400682 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /*
686 * Execute the method associated with the GPE
687 * NOTE: Level-triggered GPEs are cleared after the method completes.
688 */
Bob Moore958dd242006-05-12 17:12:00 -0400689 status = acpi_os_execute(OSL_GPE_HANDLER,
690 acpi_ev_asynch_execute_gpe_method,
691 gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400692 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500693 ACPI_EXCEPTION((AE_INFO, status,
694 "Unable to queue handler for GPE[%2X] - event disabled",
695 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 break;
698
699 default:
700
701 /* No handler or method to run! */
702
Bob Mooreb8e4d892006-01-27 16:43:00 -0500703 ACPI_ERROR((AE_INFO,
704 "No handler or method for GPE[%2X], disabling event",
705 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800708 * Disable the GPE. The GPE will remain disabled until the ACPICA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * Core Subsystem is restarted, or a handler is installed.
710 */
Len Brown4be44fc2005-08-05 00:44:28 -0400711 status = acpi_ev_disable_gpe(gpe_event_info);
712 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500713 ACPI_EXCEPTION((AE_INFO, status,
714 "Unable to disable GPE[%2X]",
715 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400716 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 break;
719 }
720
Bob Moore50eca3e2005-09-30 19:03:00 -0400721 return_UINT32(ACPI_INTERRUPT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}