blob: deb26f4c6623bc182246a2691e87c62e59a84614 [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
55/*******************************************************************************
56 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * FUNCTION: acpi_ev_update_gpe_enable_masks
58 *
59 * PARAMETERS: gpe_event_info - GPE to update
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 * RETURN: Status
62 *
Lin Ming0f849d22010-04-06 14:52:37 +080063 * DESCRIPTION: Updates GPE register enable masks based upon whether there are
64 * references (either wake or run) to this GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
66 ******************************************************************************/
67
68acpi_status
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +010069acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Len Brown4be44fc2005-08-05 00:44:28 -040071 struct acpi_gpe_register_info *gpe_register_info;
72 u8 register_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Bob Mooreb229cf92006-04-21 17:15:00 -040074 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_masks);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 gpe_register_info = gpe_event_info->register_info;
77 if (!gpe_register_info) {
Len Brown4be44fc2005-08-05 00:44:28 -040078 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Bob Moored4913dc2009-03-06 10:05:18 +080080
Alexey Starikovskiy69874162007-02-02 19:48:19 +030081 register_bit = (u8)
82 (1 <<
83 (gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Lin Ming0f849d22010-04-06 14:52:37 +080085 /* Clear the wake/run bits up front */
86
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +010087 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake, register_bit);
88 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Lin Ming0f849d22010-04-06 14:52:37 +080090 /* Set the mask bits 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) {
93 ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
94 }
95
96 if (gpe_event_info->wakeup_count) {
Len Brown4be44fc2005-08-05 00:44:28 -040097 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
Lin Ming0f849d22010-04-06 14:52:37 +080098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Len Brown4be44fc2005-08-05 00:44:28 -0400100 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*******************************************************************************
104 *
105 * FUNCTION: acpi_ev_enable_gpe
106 *
107 * PARAMETERS: gpe_event_info - GPE to enable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
109 * RETURN: Status
110 *
Lin Ming0f849d22010-04-06 14:52:37 +0800111 * DESCRIPTION: Hardware-enable a GPE. Always enables the GPE, regardless
112 * of type or number of references.
113 *
114 * Note: The GPE lock should be already acquired when this function is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
116 ******************************************************************************/
117
Rafael J. Wysockicbbc0de2010-02-24 00:52:08 +0100118acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Len Brown4be44fc2005-08-05 00:44:28 -0400120 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Lin Ming0f849d22010-04-06 14:52:37 +0800122
Bob Mooreb229cf92006-04-21 17:15:00 -0400123 ACPI_FUNCTION_TRACE(ev_enable_gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Lin Ming0f849d22010-04-06 14:52:37 +0800125
126 /*
127 * We will only allow a GPE to be enabled if it has either an
128 * associated method (_Lxx/_Exx) or a handler. Otherwise, the
129 * GPE will be immediately disabled by acpi_ev_gpe_dispatch the
130 * first time it fires.
131 */
132 if (!(gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)) {
133 return_ACPI_STATUS(AE_NO_HANDLER);
134 }
135
136 /* Ensure the HW enable masks are current */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100138 status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
Lin Ming0f849d22010-04-06 14:52:37 +0800139 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400140 return_ACPI_STATUS(status);
Lin Ming0f849d22010-04-06 14:52:37 +0800141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Lin Ming0f849d22010-04-06 14:52:37 +0800143 /* Clear the GPE (of stale events) */
144
Rafael J. Wysockibf02bd22010-03-16 23:21:55 +0100145 status = acpi_hw_clear_gpe(gpe_event_info);
Lin Ming0f849d22010-04-06 14:52:37 +0800146 if (ACPI_FAILURE(status)) {
Rafael J. Wysockibf02bd22010-03-16 23:21:55 +0100147 return_ACPI_STATUS(status);
Lin Ming0f849d22010-04-06 14:52:37 +0800148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Rafael J. Wysockibf02bd22010-03-16 23:21:55 +0100150 /* Enable the requested GPE */
Lin Ming0f849d22010-04-06 14:52:37 +0800151
Rafael J. Wysockibf02bd22010-03-16 23:21:55 +0100152 status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
153 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*******************************************************************************
157 *
158 * FUNCTION: acpi_ev_disable_gpe
159 *
160 * PARAMETERS: gpe_event_info - GPE to disable
161 *
162 * RETURN: Status
163 *
Lin Ming0f849d22010-04-06 14:52:37 +0800164 * DESCRIPTION: Hardware-disable a GPE. Always disables the requested GPE,
165 * regardless of the type or number of references.
166 *
167 * Note: The GPE lock should be already acquired when this function is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 *
169 ******************************************************************************/
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Len Brown4be44fc2005-08-05 00:44:28 -0400173 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Bob Mooreb229cf92006-04-21 17:15:00 -0400175 ACPI_FUNCTION_TRACE(ev_disable_gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Bob Mooree38e8a02008-06-13 08:28:55 +0800178 /*
Lin Ming0f849d22010-04-06 14:52:37 +0800179 * Note: Always disable the GPE, even if we think that that it is already
180 * disabled. It is possible that the AML or some other code has enabled
181 * the GPE behind our back.
182 */
183
184 /* Ensure the HW enable masks are current */
185
186 status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
187 if (ACPI_FAILURE(status)) {
188 return_ACPI_STATUS(status);
189 }
190
191 /*
192 * Always H/W disable this GPE, even if we don't know the GPE type.
193 * Simply clear the enable bit for this particular GPE, but do not
194 * write out the current GPE enable mask since this may inadvertently
195 * enable GPEs too early. An example is a rogue GPE that has arrived
196 * during ACPICA initialization - possibly because AML or other code
197 * has enabled the GPE.
Bob Mooree38e8a02008-06-13 08:28:55 +0800198 */
199 status = acpi_hw_low_disable_gpe(gpe_event_info);
200 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Lin Ming0f849d22010-04-06 14:52:37 +0800203
204/*******************************************************************************
205 *
206 * FUNCTION: acpi_ev_low_get_gpe_info
207 *
208 * PARAMETERS: gpe_number - Raw GPE number
209 * gpe_block - A GPE info block
210 *
211 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
212 * is not within the specified GPE block)
213 *
214 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
215 * the low-level implementation of ev_get_gpe_event_info.
216 *
217 ******************************************************************************/
218
219struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
220 struct acpi_gpe_block_info
221 *gpe_block)
222{
223 u32 gpe_index;
224
225 /*
226 * Validate that the gpe_number is within the specified gpe_block.
227 * (Two steps)
228 */
229 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
230 return (NULL);
231 }
232
233 gpe_index = gpe_number - gpe_block->block_base_number;
234 if (gpe_index >= gpe_block->gpe_count) {
235 return (NULL);
236 }
237
238 return (&gpe_block->event_info[gpe_index]);
239}
240
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/*******************************************************************************
243 *
244 * FUNCTION: acpi_ev_get_gpe_event_info
245 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800246 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * gpe_number - Raw GPE number
248 *
249 * RETURN: A GPE event_info struct. NULL if not a valid GPE
250 *
251 * DESCRIPTION: Returns the event_info struct associated with this GPE.
252 * Validates the gpe_block and the gpe_number
253 *
254 * Should be called only when the GPE lists are semaphore locked
255 * and not subject to change.
256 *
257 ******************************************************************************/
258
Len Brown4be44fc2005-08-05 00:44:28 -0400259struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
260 u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Len Brown4be44fc2005-08-05 00:44:28 -0400262 union acpi_operand_object *obj_desc;
Lin Ming0f849d22010-04-06 14:52:37 +0800263 struct acpi_gpe_event_info *gpe_info;
Bob Moore67a119f2008-06-10 13:42:13 +0800264 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Len Brown4be44fc2005-08-05 00:44:28 -0400266 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* A NULL gpe_block means use the FADT-defined GPE block(s) */
269
270 if (!gpe_device) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
273
274 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
Lin Ming0f849d22010-04-06 14:52:37 +0800275 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
276 acpi_gbl_gpe_fadt_blocks
277 [i]);
278 if (gpe_info) {
279 return (gpe_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 }
282
283 /* The gpe_number was not in the range of either FADT GPE block */
284
285 return (NULL);
286 }
287
288 /* A Non-NULL gpe_device means this is a GPE Block Device */
289
Len Brownfd350942007-05-09 23:34:35 -0400290 obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
291 gpe_device);
Len Brown4be44fc2005-08-05 00:44:28 -0400292 if (!obj_desc || !obj_desc->device.gpe_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return (NULL);
294 }
295
Lin Ming0f849d22010-04-06 14:52:37 +0800296 return (acpi_ev_low_get_gpe_info
297 (gpe_number, obj_desc->device.gpe_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*******************************************************************************
301 *
302 * FUNCTION: acpi_ev_gpe_detect
303 *
304 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
305 * Can have multiple GPE blocks attached.
306 *
307 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
308 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800309 * DESCRIPTION: Detect if any GP events have occurred. This function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * executed at interrupt level.
311 *
312 ******************************************************************************/
313
Len Brown4be44fc2005-08-05 00:44:28 -0400314u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Len Brown4be44fc2005-08-05 00:44:28 -0400316 acpi_status status;
317 struct acpi_gpe_block_info *gpe_block;
Bob Moore08978312005-10-21 00:00:00 -0400318 struct acpi_gpe_register_info *gpe_register_info;
319 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
320 u8 enabled_status_byte;
321 u32 status_reg;
322 u32 enable_reg;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500323 acpi_cpu_flags flags;
Bob Moore67a119f2008-06-10 13:42:13 +0800324 u32 i;
325 u32 j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Bob Mooreb229cf92006-04-21 17:15:00 -0400327 ACPI_FUNCTION_NAME(ev_gpe_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /* Check for the case where there are no GPEs */
330
331 if (!gpe_xrupt_list) {
332 return (int_status);
333 }
334
Bob Moore967440e32006-06-23 17:04:00 -0400335 /*
336 * We need to obtain the GPE lock for both the data structs and registers
Bob Moore9f15fc62008-11-12 16:01:56 +0800337 * Note: Not necessary to obtain the hardware lock, since the GPE
338 * registers are owned by the gpe_lock.
Bob Moore967440e32006-06-23 17:04:00 -0400339 */
Len Brown4be44fc2005-08-05 00:44:28 -0400340 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Bob Moore4c90ece2006-06-08 16:29:00 -0400341
342 /* Examine all GPE blocks attached to this interrupt level */
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 gpe_block = gpe_xrupt_list->gpe_block_list_head;
345 while (gpe_block) {
346 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800347 * Read all of the 8-bit GPE status and enable registers in this GPE
348 * block, saving all of them. Find all currently active GP events.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
350 for (i = 0; i < gpe_block->register_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Get the next status/enable pair */
353
354 gpe_register_info = &gpe_block->register_info[i];
355
356 /* Read the Status Register */
357
Len Brown4be44fc2005-08-05 00:44:28 -0400358 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800359 acpi_hw_read(&status_reg,
360 &gpe_register_info->status_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400361 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto unlock_and_exit;
363 }
364
365 /* Read the Enable Register */
366
Len Brown4be44fc2005-08-05 00:44:28 -0400367 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800368 acpi_hw_read(&enable_reg,
369 &gpe_register_info->enable_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400370 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto unlock_and_exit;
372 }
373
Len Brown4be44fc2005-08-05 00:44:28 -0400374 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
375 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
376 gpe_register_info->base_gpe_number,
377 status_reg, enable_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Robert Moore44f6c012005-04-18 22:49:35 -0400379 /* Check if there is anything active at all in this register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 enabled_status_byte = (u8) (status_reg & enable_reg);
382 if (!enabled_status_byte) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* No active GPEs in this register, move on */
385
386 continue;
387 }
388
389 /* Now look at the individual GPEs in this byte register */
390
391 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* Examine one GPE bit */
394
Alexey Starikovskiy69874162007-02-02 19:48:19 +0300395 if (enabled_status_byte & (1 << j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /*
397 * Found an active GPE. Dispatch the event to a handler
398 * or method.
399 */
Len Brown4be44fc2005-08-05 00:44:28 -0400400 int_status |=
401 acpi_ev_gpe_dispatch(&gpe_block->
Bob Moore67a119f2008-06-10 13:42:13 +0800402 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 }
405 }
406
407 gpe_block = gpe_block->next;
408 }
409
Len Brown4be44fc2005-08-05 00:44:28 -0400410 unlock_and_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Len Brown4be44fc2005-08-05 00:44:28 -0400412 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return (int_status);
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*******************************************************************************
417 *
418 * FUNCTION: acpi_ev_asynch_execute_gpe_method
419 *
420 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
421 *
422 * RETURN: None
423 *
Bob Moore41195322006-05-26 16:36:00 -0400424 * DESCRIPTION: Perform the actual execution of a GPE control method. This
425 * function is called from an invocation of acpi_os_execute and
426 * therefore does NOT execute at interrupt level - so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * the control method itself is not executed in the context of
428 * an interrupt handler.
429 *
430 ******************************************************************************/
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300431static void acpi_ev_asynch_enable_gpe(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Len Brown4be44fc2005-08-05 00:44:28 -0400433static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Len Brown4be44fc2005-08-05 00:44:28 -0400435 struct acpi_gpe_event_info *gpe_event_info = (void *)context;
Len Brown4be44fc2005-08-05 00:44:28 -0400436 acpi_status status;
437 struct acpi_gpe_event_info local_gpe_event_info;
Bob Moore41195322006-05-26 16:36:00 -0400438 struct acpi_evaluate_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Bob Mooreb229cf92006-04-21 17:15:00 -0400440 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Len Brown4be44fc2005-08-05 00:44:28 -0400442 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
443 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return_VOID;
445 }
446
447 /* Must revalidate the gpe_number/gpe_block */
448
Len Brown4be44fc2005-08-05 00:44:28 -0400449 if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
450 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return_VOID;
452 }
453
Lin Ming0f849d22010-04-06 14:52:37 +0800454 /* Update the GPE register masks for return to enabled state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Rafael J. Wysockicbbc0de2010-02-24 00:52:08 +0100456 (void)acpi_ev_update_gpe_enable_masks(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800459 * Take a snapshot of the GPE info for this level - we copy the info to
460 * prevent a race condition with remove_handler/remove_block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Len Brown4be44fc2005-08-05 00:44:28 -0400462 ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
463 sizeof(struct acpi_gpe_event_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Len Brown4be44fc2005-08-05 00:44:28 -0400465 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
466 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return_VOID;
468 }
469
470 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800471 * Must check for control method type dispatch one more time to avoid a
472 * race with ev_gpe_install_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
Robert Moore44f6c012005-04-18 22:49:35 -0400474 if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400475 ACPI_GPE_DISPATCH_METHOD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Bob Moore41195322006-05-26 16:36:00 -0400477 /* Allocate the evaluation information block */
478
479 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
480 if (!info) {
481 status = AE_NO_MEMORY;
482 } else {
483 /*
484 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
485 * control method that corresponds to this GPE
486 */
487 info->prefix_node =
488 local_gpe_event_info.dispatch.method_node;
Bob Moore41195322006-05-26 16:36:00 -0400489 info->flags = ACPI_IGNORE_RETURN_VALUE;
490
491 status = acpi_ns_evaluate(info);
492 ACPI_FREE(info);
493 }
494
Len Brown4be44fc2005-08-05 00:44:28 -0400495 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500496 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore2e420052007-02-02 19:48:18 +0300497 "while evaluating GPE method [%4.4s]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500498 acpi_ut_get_node_name
499 (local_gpe_event_info.dispatch.
Bob Moore4c90ece2006-06-08 16:29:00 -0400500 method_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 }
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300503 /* Defer enabling of GPE until all notify handlers are done */
504 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
505 gpe_event_info);
506 return_VOID;
507}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300509static void acpi_ev_asynch_enable_gpe(void *context)
510{
511 struct acpi_gpe_event_info *gpe_event_info = context;
512 acpi_status status;
513 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400514 ACPI_GPE_LEVEL_TRIGGERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800516 * GPE is level-triggered, we clear the GPE status bit after handling
517 * the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300519 status = acpi_hw_clear_gpe(gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400520 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return_VOID;
522 }
523 }
524
525 /* Enable this GPE */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300526 (void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return_VOID;
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/*******************************************************************************
531 *
532 * FUNCTION: acpi_ev_gpe_dispatch
533 *
Robert Moore44f6c012005-04-18 22:49:35 -0400534 * PARAMETERS: gpe_event_info - Info for this GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * gpe_number - Number relative to the parent GPE block
536 *
537 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
538 *
539 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
540 * or method (e.g. _Lxx/_Exx) handler.
541 *
542 * This function executes at interrupt level.
543 *
544 ******************************************************************************/
545
546u32
Len Brown4be44fc2005-08-05 00:44:28 -0400547acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Len Brown4be44fc2005-08-05 00:44:28 -0400549 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Bob Mooreb229cf92006-04-21 17:15:00 -0400551 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Len Brown5229e872008-02-06 01:26:55 -0500553 acpi_os_gpe_count(gpe_number);
Bob Moorefdffb722007-02-02 19:48:19 +0300554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800556 * If edge-triggered, clear the GPE status bit now. Note that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * level-triggered events are cleared after the GPE is serviced.
558 */
Robert Moore44f6c012005-04-18 22:49:35 -0400559 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400560 ACPI_GPE_EDGE_TRIGGERED) {
561 status = acpi_hw_clear_gpe(gpe_event_info);
562 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500563 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800564 "Unable to clear GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500565 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400566 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
Bob Moorec5a71562007-02-02 19:48:19 +0300571 * Dispatch the GPE to either an installed handler, or the control method
572 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
573 * it and do not attempt to run the method. If there is neither a handler
574 * nor a method, we disable this GPE to prevent further such pointless
575 * events from firing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
577 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
578 case ACPI_GPE_DISPATCH_HANDLER:
579
580 /*
581 * Invoke the installed handler (at interrupt level)
Bob Moore9f15fc62008-11-12 16:01:56 +0800582 * Ignore return status for now.
583 * TBD: leave GPE disabled on error?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
Len Brown4be44fc2005-08-05 00:44:28 -0400585 (void)gpe_event_info->dispatch.handler->address(gpe_event_info->
586 dispatch.
587 handler->
588 context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* It is now safe to clear level-triggered events. */
591
Robert Moore44f6c012005-04-18 22:49:35 -0400592 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400593 ACPI_GPE_LEVEL_TRIGGERED) {
594 status = acpi_hw_clear_gpe(gpe_event_info);
595 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500596 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800597 "Unable to clear GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500598 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400599 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 }
602 break;
603
604 case ACPI_GPE_DISPATCH_METHOD:
605
606 /*
Bob Moorec5a71562007-02-02 19:48:19 +0300607 * Disable the GPE, so it doesn't keep firing before the method has a
608 * chance to run (it runs asynchronously with interrupts enabled).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Len Brown4be44fc2005-08-05 00:44:28 -0400610 status = acpi_ev_disable_gpe(gpe_event_info);
611 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500612 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800613 "Unable to disable GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500614 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400615 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 /*
619 * Execute the method associated with the GPE
620 * NOTE: Level-triggered GPEs are cleared after the method completes.
621 */
Bob Moore958dd242006-05-12 17:12:00 -0400622 status = acpi_os_execute(OSL_GPE_HANDLER,
623 acpi_ev_asynch_execute_gpe_method,
624 gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400625 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500626 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800627 "Unable to queue handler for GPE[0x%2X] - event disabled",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500628 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 break;
631
632 default:
633
Lin Ming0f849d22010-04-06 14:52:37 +0800634 /*
635 * No handler or method to run!
636 * 03/2010: This case should no longer be possible. We will not allow
637 * a GPE to be enabled if it has no handler or method.
638 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500639 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800640 "No handler or method for GPE[0x%2X], disabling event",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500641 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /*
Lin Ming0f849d22010-04-06 14:52:37 +0800644 * Disable the GPE. The GPE will remain disabled a handler
645 * is installed or ACPICA is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Len Brown4be44fc2005-08-05 00:44:28 -0400647 status = acpi_ev_disable_gpe(gpe_event_info);
648 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500649 ACPI_EXCEPTION((AE_INFO, status,
Bob Mooref6a22b02010-03-05 17:56:40 +0800650 "Unable to disable GPE[0x%2X]",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500651 gpe_number));
Bob Moore50eca3e2005-09-30 19:03:00 -0400652 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 break;
655 }
656
Bob Moore50eca3e2005-09-30 19:03:00 -0400657 return_UINT32(ACPI_INTERRUPT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}