blob: fccdfb2f73df515e49578ea3d4d849f31eadbdd3 [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 Moorefbb7a2d2014-02-08 09:42:25 +08008 * Copyright (C) 2000 - 2014, 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")
Bob Moore33620c52012-02-14 18:14:27 +080051#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
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
Feng Tang1d94e1e2012-08-17 11:10:02 +080083 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080085 /* Clear the run bit up front */
Lin Ming0f849d22010-04-06 14:52:37 +080086
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +010087 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Rafael J. Wysockia44061a2010-07-01 10:11:45 +080089 /* Set the mask bit only if there are references to this GPE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Lin Ming0f849d22010-04-06 14:52:37 +080091 if (gpe_event_info->runtime_count) {
Lv Zheng1f86e8c2012-10-31 02:25:45 +000092 ACPI_SET_BIT(gpe_register_info->enable_for_run,
93 (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 *
Lv Zhengf19f1a72014-10-10 10:39:39 +0800103 * PARAMETERS: gpe_event_info - GPE to enable
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800104 *
105 * RETURN: Status
106 *
Lin Mingda503372010-12-13 13:39:37 +0800107 * DESCRIPTION: Clear a GPE of stale events and enable it.
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800108 *
109 ******************************************************************************/
Lv Zhengf19f1a72014-10-10 10:39:39 +0800110
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000111acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800112{
113 acpi_status status;
114
115 ACPI_FUNCTION_TRACE(ev_enable_gpe);
116
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800117 /* Clear the GPE (of stale events) */
Lv Zhengf19f1a72014-10-10 10:39:39 +0800118
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800119 status = acpi_hw_clear_gpe(gpe_event_info);
120 if (ACPI_FAILURE(status)) {
121 return_ACPI_STATUS(status);
122 }
123
124 /* Enable the requested GPE */
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800125
Rafael J. Wysockic50f13c2014-12-01 23:50:16 +0100126 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE_SAVE);
Rafael J. Wysocki3bd741b2010-07-01 11:01:12 +0800127 return_ACPI_STATUS(status);
128}
129
Lin Ming0f849d22010-04-06 14:52:37 +0800130/*******************************************************************************
131 *
Lin Ming3a378982010-12-13 13:36:15 +0800132 * FUNCTION: acpi_ev_add_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200133 *
Lin Mingda503372010-12-13 13:39:37 +0800134 * PARAMETERS: gpe_event_info - Add a reference to this GPE
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200135 *
136 * RETURN: Status
137 *
138 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
139 * hardware-enabled.
140 *
141 ******************************************************************************/
142
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000143acpi_status
144acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200145{
146 acpi_status status = AE_OK;
147
Lin Ming3a378982010-12-13 13:36:15 +0800148 ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
149
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200150 if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
151 return_ACPI_STATUS(AE_LIMIT);
152 }
153
154 gpe_event_info->runtime_count++;
155 if (gpe_event_info->runtime_count == 1) {
Lin Mingda503372010-12-13 13:39:37 +0800156
157 /* Enable on first reference */
158
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200159 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
160 if (ACPI_SUCCESS(status)) {
161 status = acpi_ev_enable_gpe(gpe_event_info);
162 }
163
164 if (ACPI_FAILURE(status)) {
165 gpe_event_info->runtime_count--;
166 }
167 }
168
169 return_ACPI_STATUS(status);
170}
171
172/*******************************************************************************
173 *
Lin Ming3a378982010-12-13 13:36:15 +0800174 * FUNCTION: acpi_ev_remove_gpe_reference
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200175 *
Lin Mingda503372010-12-13 13:39:37 +0800176 * PARAMETERS: gpe_event_info - Remove a reference to this GPE
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200177 *
178 * RETURN: Status
179 *
180 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
181 * removed, the GPE is hardware-disabled.
182 *
183 ******************************************************************************/
184
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000185acpi_status
186acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200187{
188 acpi_status status = AE_OK;
189
Lin Ming3a378982010-12-13 13:36:15 +0800190 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
191
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200192 if (!gpe_event_info->runtime_count) {
193 return_ACPI_STATUS(AE_LIMIT);
194 }
195
196 gpe_event_info->runtime_count--;
197 if (!gpe_event_info->runtime_count) {
Lin Mingda503372010-12-13 13:39:37 +0800198
199 /* Disable on last reference */
200
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200201 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
202 if (ACPI_SUCCESS(status)) {
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000203 status =
204 acpi_hw_low_set_gpe(gpe_event_info,
Rafael J. Wysockic50f13c2014-12-01 23:50:16 +0100205 ACPI_GPE_DISABLE_SAVE);
Rafael J. Wysocki28f4f8a2010-08-03 23:55:14 +0200206 }
207
208 if (ACPI_FAILURE(status)) {
209 gpe_event_info->runtime_count++;
210 }
211 }
212
213 return_ACPI_STATUS(status);
214}
215
216/*******************************************************************************
217 *
Lin Ming0f849d22010-04-06 14:52:37 +0800218 * FUNCTION: acpi_ev_low_get_gpe_info
219 *
220 * PARAMETERS: gpe_number - Raw GPE number
221 * gpe_block - A GPE info block
222 *
223 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
224 * is not within the specified GPE block)
225 *
226 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
227 * the low-level implementation of ev_get_gpe_event_info.
228 *
229 ******************************************************************************/
230
231struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
232 struct acpi_gpe_block_info
233 *gpe_block)
234{
235 u32 gpe_index;
236
237 /*
238 * Validate that the gpe_number is within the specified gpe_block.
239 * (Two steps)
240 */
241 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
242 return (NULL);
243 }
244
245 gpe_index = gpe_number - gpe_block->block_base_number;
246 if (gpe_index >= gpe_block->gpe_count) {
247 return (NULL);
248 }
249
250 return (&gpe_block->event_info[gpe_index]);
251}
252
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*******************************************************************************
255 *
256 * FUNCTION: acpi_ev_get_gpe_event_info
257 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800258 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * gpe_number - Raw GPE number
260 *
261 * RETURN: A GPE event_info struct. NULL if not a valid GPE
262 *
263 * DESCRIPTION: Returns the event_info struct associated with this GPE.
264 * Validates the gpe_block and the gpe_number
265 *
266 * Should be called only when the GPE lists are semaphore locked
267 * and not subject to change.
268 *
269 ******************************************************************************/
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
272 u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Len Brown4be44fc2005-08-05 00:44:28 -0400274 union acpi_operand_object *obj_desc;
Lin Ming0f849d22010-04-06 14:52:37 +0800275 struct acpi_gpe_event_info *gpe_info;
Bob Moore67a119f2008-06-10 13:42:13 +0800276 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Len Brown4be44fc2005-08-05 00:44:28 -0400278 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Bob Moore186c3072010-04-27 11:32:28 +0800280 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (!gpe_device) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
285
286 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
Lin Ming0f849d22010-04-06 14:52:37 +0800287 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
288 acpi_gbl_gpe_fadt_blocks
289 [i]);
290 if (gpe_info) {
291 return (gpe_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 }
294
295 /* The gpe_number was not in the range of either FADT GPE block */
296
297 return (NULL);
298 }
299
300 /* A Non-NULL gpe_device means this is a GPE Block Device */
301
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000302 obj_desc =
303 acpi_ns_get_attached_object((struct acpi_namespace_node *)
Len Brownfd350942007-05-09 23:34:35 -0400304 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
Lv Zhengf19f1a72014-10-10 10:39:39 +0800327u32 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;
Lv Zheng779ba5a2015-02-05 15:20:22 +0800331 struct acpi_namespace_node *gpe_device;
Bob Moore08978312005-10-21 00:00:00 -0400332 struct acpi_gpe_register_info *gpe_register_info;
Lv Zheng779ba5a2015-02-05 15:20:22 +0800333 struct acpi_gpe_event_info *gpe_event_info;
334 u32 gpe_number;
Bob Moore08978312005-10-21 00:00:00 -0400335 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
336 u8 enabled_status_byte;
337 u32 status_reg;
338 u32 enable_reg;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500339 acpi_cpu_flags flags;
Bob Moore67a119f2008-06-10 13:42:13 +0800340 u32 i;
341 u32 j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Bob Mooreb229cf92006-04-21 17:15:00 -0400343 ACPI_FUNCTION_NAME(ev_gpe_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Check for the case where there are no GPEs */
346
347 if (!gpe_xrupt_list) {
348 return (int_status);
349 }
350
Bob Moore967440e32006-06-23 17:04:00 -0400351 /*
352 * We need to obtain the GPE lock for both the data structs and registers
Bob Moore9f15fc62008-11-12 16:01:56 +0800353 * Note: Not necessary to obtain the hardware lock, since the GPE
354 * registers are owned by the gpe_lock.
Bob Moore967440e32006-06-23 17:04:00 -0400355 */
Len Brown4be44fc2005-08-05 00:44:28 -0400356 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Bob Moore4c90ece2006-06-08 16:29:00 -0400357
358 /* Examine all GPE blocks attached to this interrupt level */
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 gpe_block = gpe_xrupt_list->gpe_block_list_head;
361 while (gpe_block) {
Lv Zheng779ba5a2015-02-05 15:20:22 +0800362 gpe_device = gpe_block->node;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800365 * Read all of the 8-bit GPE status and enable registers in this GPE
366 * block, saving all of them. Find all currently active GP events.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
368 for (i = 0; i < gpe_block->register_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Get the next status/enable pair */
371
372 gpe_register_info = &gpe_block->register_info[i];
373
Lin Ming6dfad332011-02-14 15:29:34 +0800374 /*
375 * Optimization: If there are no GPEs enabled within this
376 * register, we can safely ignore the entire register.
377 */
378 if (!(gpe_register_info->enable_for_run |
379 gpe_register_info->enable_for_wake)) {
Bob Mooree1154eb2012-08-17 10:55:14 +0800380 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
Bob Moore7505da42014-04-30 10:06:15 +0800381 "Ignore disabled registers for GPE %02X-%02X: "
Bob Mooree1154eb2012-08-17 10:55:14 +0800382 "RunEnable=%02X, WakeEnable=%02X\n",
383 gpe_register_info->
384 base_gpe_number,
385 gpe_register_info->
386 base_gpe_number +
387 (ACPI_GPE_REGISTER_WIDTH - 1),
388 gpe_register_info->
389 enable_for_run,
390 gpe_register_info->
391 enable_for_wake));
Lin Ming6dfad332011-02-14 15:29:34 +0800392 continue;
393 }
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Read the Status Register */
396
Len Brown4be44fc2005-08-05 00:44:28 -0400397 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800398 acpi_hw_read(&status_reg,
399 &gpe_register_info->status_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400400 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto unlock_and_exit;
402 }
403
404 /* Read the Enable Register */
405
Len Brown4be44fc2005-08-05 00:44:28 -0400406 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800407 acpi_hw_read(&enable_reg,
408 &gpe_register_info->enable_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400409 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto unlock_and_exit;
411 }
412
Len Brown4be44fc2005-08-05 00:44:28 -0400413 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
Bob Moore7505da42014-04-30 10:06:15 +0800414 "Read registers for GPE %02X-%02X: Status=%02X, Enable=%02X, "
Bob Mooree1154eb2012-08-17 10:55:14 +0800415 "RunEnable=%02X, WakeEnable=%02X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400416 gpe_register_info->base_gpe_number,
Bob Mooree1154eb2012-08-17 10:55:14 +0800417 gpe_register_info->base_gpe_number +
418 (ACPI_GPE_REGISTER_WIDTH - 1),
419 status_reg, enable_reg,
420 gpe_register_info->enable_for_run,
421 gpe_register_info->enable_for_wake));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Robert Moore44f6c012005-04-18 22:49:35 -0400423 /* Check if there is anything active at all in this register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Lv Zhengf19f1a72014-10-10 10:39:39 +0800425 enabled_status_byte = (u8)(status_reg & enable_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!enabled_status_byte) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* No active GPEs in this register, move on */
429
430 continue;
431 }
432
433 /* Now look at the individual GPEs in this byte register */
434
435 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Examine one GPE bit */
438
Lv Zheng779ba5a2015-02-05 15:20:22 +0800439 gpe_event_info =
440 &gpe_block->
441 event_info[((acpi_size) i *
442 ACPI_GPE_REGISTER_WIDTH) + j];
443 gpe_number =
444 j + gpe_register_info->base_gpe_number;
445
Alexey Starikovskiy69874162007-02-02 19:48:19 +0300446 if (enabled_status_byte & (1 << j)) {
Lv Zheng779ba5a2015-02-05 15:20:22 +0800447
448 /* Invoke global event handler if present */
449
450 acpi_gpe_count++;
451 if (acpi_gbl_global_event_handler) {
452 acpi_gbl_global_event_handler
453 (ACPI_EVENT_TYPE_GPE,
454 gpe_device, gpe_number,
455 acpi_gbl_global_event_handler_context);
456 }
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /*
459 * Found an active GPE. Dispatch the event to a handler
460 * or method.
461 */
Len Brown4be44fc2005-08-05 00:44:28 -0400462 int_status |=
Lv Zheng779ba5a2015-02-05 15:20:22 +0800463 acpi_ev_gpe_dispatch(gpe_device,
464 gpe_event_info,
465 gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 }
468 }
469
470 gpe_block = gpe_block->next;
471 }
472
Lv Zheng10622bf2013-10-29 09:30:02 +0800473unlock_and_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Len Brown4be44fc2005-08-05 00:44:28 -0400475 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return (int_status);
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*******************************************************************************
480 *
481 * FUNCTION: acpi_ev_asynch_execute_gpe_method
482 *
483 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
484 *
485 * RETURN: None
486 *
Bob Moore41195322006-05-26 16:36:00 -0400487 * DESCRIPTION: Perform the actual execution of a GPE control method. This
488 * function is called from an invocation of acpi_os_execute and
489 * therefore does NOT execute at interrupt level - so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * the control method itself is not executed in the context of
491 * an interrupt handler.
492 *
493 ******************************************************************************/
494
Len Brown4be44fc2005-08-05 00:44:28 -0400495static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Lin Ming5a284cd2010-12-13 13:39:07 +0800497 struct acpi_gpe_event_info *gpe_event_info = context;
David E. Boxb7be6882015-02-05 15:20:07 +0800498 acpi_status status = AE_OK;
Bob Moore41195322006-05-26 16:36:00 -0400499 struct acpi_evaluate_info *info;
Bob Moore5816b342012-06-29 10:04:17 +0800500 struct acpi_gpe_notify_info *notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Bob Mooreb229cf92006-04-21 17:15:00 -0400502 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Lin Mingbba63a22010-12-13 13:39:17 +0800504 /* Do the correct dispatch - normal method or implicit notify */
505
Lv Zheng7c433122015-02-05 15:20:29 +0800506 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
Lin Mingbba63a22010-12-13 13:39:17 +0800507 case ACPI_GPE_DISPATCH_NOTIFY:
Lin Mingbba63a22010-12-13 13:39:17 +0800508 /*
509 * Implicit notify.
510 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
511 * NOTE: the request is queued for execution after this method
512 * completes. The notify handlers are NOT invoked synchronously
513 * from this thread -- because handlers may in turn run other
514 * control methods.
Bob Moore5816b342012-06-29 10:04:17 +0800515 *
516 * June 2012: Expand implicit notify mechanism to support
517 * notifies on multiple device objects.
Lin Mingbba63a22010-12-13 13:39:17 +0800518 */
Lv Zhengb18da582015-02-05 15:20:01 +0800519 notify = gpe_event_info->dispatch.notify_list;
Bob Moore5816b342012-06-29 10:04:17 +0800520 while (ACPI_SUCCESS(status) && notify) {
521 status =
522 acpi_ev_queue_notify_request(notify->device_node,
523 ACPI_NOTIFY_DEVICE_WAKE);
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100524
Bob Moore5816b342012-06-29 10:04:17 +0800525 notify = notify->next;
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100526 }
527
Lin Mingbba63a22010-12-13 13:39:17 +0800528 break;
529
530 case ACPI_GPE_DISPATCH_METHOD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Bob Moore41195322006-05-26 16:36:00 -0400532 /* Allocate the evaluation information block */
533
534 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
535 if (!info) {
536 status = AE_NO_MEMORY;
537 } else {
538 /*
Lv Zheng75c80442012-12-19 05:36:49 +0000539 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
540 * _Lxx/_Exx control method that corresponds to this GPE
Bob Moore41195322006-05-26 16:36:00 -0400541 */
542 info->prefix_node =
Lv Zhengb18da582015-02-05 15:20:01 +0800543 gpe_event_info->dispatch.method_node;
Bob Moore41195322006-05-26 16:36:00 -0400544 info->flags = ACPI_IGNORE_RETURN_VALUE;
545
546 status = acpi_ns_evaluate(info);
547 ACPI_FREE(info);
548 }
549
Len Brown4be44fc2005-08-05 00:44:28 -0400550 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500551 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore2e420052007-02-02 19:48:18 +0300552 "while evaluating GPE method [%4.4s]",
Lv Zhengb18da582015-02-05 15:20:01 +0800553 acpi_ut_get_node_name(gpe_event_info->
554 dispatch.
555 method_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Lin Mingbba63a22010-12-13 13:39:17 +0800557 break;
558
559 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000560
Lv Zhengb18da582015-02-05 15:20:01 +0800561 goto error_exit; /* Should never happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Lin Ming5a284cd2010-12-13 13:39:07 +0800563
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300564 /* Defer enabling of GPE until all notify handlers are done */
Lin Ming5a284cd2010-12-13 13:39:07 +0800565
566 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
Lv Zhengb18da582015-02-05 15:20:01 +0800567 acpi_ev_asynch_enable_gpe, gpe_event_info);
568 if (ACPI_SUCCESS(status)) {
569 return_VOID;
Lin Ming5a284cd2010-12-13 13:39:07 +0800570 }
Lv Zhengb18da582015-02-05 15:20:01 +0800571
572error_exit:
573 acpi_ev_asynch_enable_gpe(gpe_event_info);
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300574 return_VOID;
575}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Lin Mingbba63a22010-12-13 13:39:17 +0800577
Lin Ming5a284cd2010-12-13 13:39:07 +0800578/*******************************************************************************
579 *
580 * FUNCTION: acpi_ev_asynch_enable_gpe
581 *
582 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
583 * Callback from acpi_os_execute
584 *
585 * RETURN: None
586 *
587 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
Lin Mingbba63a22010-12-13 13:39:17 +0800588 * complete (i.e., finish execution of Notify)
Lin Ming5a284cd2010-12-13 13:39:07 +0800589 *
590 ******************************************************************************/
591
592static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300593{
594 struct acpi_gpe_event_info *gpe_event_info = context;
Lv Zheng7dd88212014-12-02 08:56:55 +0800595 acpi_cpu_flags flags;
Lin Mingbba63a22010-12-13 13:39:17 +0800596
Lv Zheng7dd88212014-12-02 08:56:55 +0800597 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Lin Mingbba63a22010-12-13 13:39:17 +0800598 (void)acpi_ev_finish_gpe(gpe_event_info);
Lv Zheng7dd88212014-12-02 08:56:55 +0800599 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Lin Mingbba63a22010-12-13 13:39:17 +0800600
Lin Mingbba63a22010-12-13 13:39:17 +0800601 return;
602}
603
604
605/*******************************************************************************
606 *
607 * FUNCTION: acpi_ev_finish_gpe
608 *
609 * PARAMETERS: gpe_event_info - Info for this GPE
610 *
611 * RETURN: Status
612 *
613 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
614 * of a GPE method or a synchronous or asynchronous GPE handler.
615 *
616 ******************************************************************************/
617
Lv Zhengf19f1a72014-10-10 10:39:39 +0800618acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info * gpe_event_info)
Lin Mingbba63a22010-12-13 13:39:17 +0800619{
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300620 acpi_status status;
Lin Ming5a284cd2010-12-13 13:39:07 +0800621
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300622 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400623 ACPI_GPE_LEVEL_TRIGGERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800625 * GPE is level-triggered, we clear the GPE status bit after
626 * handling the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
Alexey Starikovskiy17bc54e2007-11-13 13:05:45 +0300628 status = acpi_hw_clear_gpe(gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400629 if (ACPI_FAILURE(status)) {
Lin Mingbba63a22010-12-13 13:39:17 +0800630 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632 }
633
Bob Moorede5668f2010-07-06 10:30:37 +0800634 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800635 * Enable this GPE, conditionally. This means that the GPE will
Rafael J. Wysockic50f13c2014-12-01 23:50:16 +0100636 * only be physically enabled if the enable_mask bit is set
Lin Mingbba63a22010-12-13 13:39:17 +0800637 * in the event_info.
Bob Moorede5668f2010-07-06 10:30:37 +0800638 */
Lin Ming3a378982010-12-13 13:36:15 +0800639 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
Lin Mingbba63a22010-12-13 13:39:17 +0800640 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Lin Mingbba63a22010-12-13 13:39:17 +0800643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/*******************************************************************************
645 *
646 * FUNCTION: acpi_ev_gpe_dispatch
647 *
Lv Zhengf19f1a72014-10-10 10:39:39 +0800648 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
649 * gpe_event_info - Info for this GPE
650 * gpe_number - Number relative to the parent GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 *
652 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
653 *
654 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
655 * or method (e.g. _Lxx/_Exx) handler.
656 *
657 * This function executes at interrupt level.
658 *
659 ******************************************************************************/
660
661u32
Lin Ming8b6cd8a2010-12-13 13:38:46 +0800662acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
Lv Zhengf19f1a72014-10-10 10:39:39 +0800663 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Len Brown4be44fc2005-08-05 00:44:28 -0400665 acpi_status status;
Lin Mingbba63a22010-12-13 13:39:17 +0800666 u32 return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Bob Mooreb229cf92006-04-21 17:15:00 -0400668 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800671 * Always disable the GPE so that it does not keep firing before
672 * any asynchronous activity completes (either from the execution
673 * of a GPE method or an asynchronous GPE handler.)
674 *
675 * If there is no handler or method to run, just disable the
676 * GPE and leave it disabled permanently to prevent further such
677 * pointless events from firing.
678 */
679 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
680 if (ACPI_FAILURE(status)) {
681 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore7505da42014-04-30 10:06:15 +0800682 "Unable to disable GPE %02X", gpe_number));
Bob Moorefd1af712013-03-08 09:22:23 +0000683 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
Lin Mingbba63a22010-12-13 13:39:17 +0800684 }
685
686 /*
Lv Zheng6ec5e122014-06-15 08:42:31 +0800687 * If edge-triggered, clear the GPE status bit now. Note that
688 * level-triggered events are cleared after the GPE is serviced.
689 */
690 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
691 ACPI_GPE_EDGE_TRIGGERED) {
692 status = acpi_hw_clear_gpe(gpe_event_info);
693 if (ACPI_FAILURE(status)) {
694 ACPI_EXCEPTION((AE_INFO, status,
695 "Unable to clear GPE %02X",
696 gpe_number));
697 (void)acpi_hw_low_set_gpe(gpe_event_info,
698 ACPI_GPE_CONDITIONAL_ENABLE);
699 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
700 }
701 }
702
703 /*
Lin Mingbba63a22010-12-13 13:39:17 +0800704 * Dispatch the GPE to either an installed handler or the control
705 * method associated with this GPE (_Lxx or _Exx). If a handler
706 * exists, we invoke it and do not attempt to run the method.
707 * If there is neither a handler nor a method, leave the GPE
708 * disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Lv Zheng7c433122015-02-05 15:20:29 +0800710 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 case ACPI_GPE_DISPATCH_HANDLER:
712
Lin Mingbba63a22010-12-13 13:39:17 +0800713 /* Invoke the installed handler (at interrupt level) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Lin Mingbba63a22010-12-13 13:39:17 +0800715 return_value =
716 gpe_event_info->dispatch.handler->address(gpe_device,
717 gpe_number,
718 gpe_event_info->
719 dispatch.handler->
720 context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Lin Mingbba63a22010-12-13 13:39:17 +0800722 /* If requested, clear (if level-triggered) and reenable the GPE */
723
724 if (return_value & ACPI_REENABLE_GPE) {
725 (void)acpi_ev_finish_gpe(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 break;
728
729 case ACPI_GPE_DISPATCH_METHOD:
Lin Mingbba63a22010-12-13 13:39:17 +0800730 case ACPI_GPE_DISPATCH_NOTIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
732 * Execute the method associated with the GPE
733 * NOTE: Level-triggered GPEs are cleared after the method completes.
734 */
Bob Moore958dd242006-05-12 17:12:00 -0400735 status = acpi_os_execute(OSL_GPE_HANDLER,
736 acpi_ev_asynch_execute_gpe_method,
737 gpe_event_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400738 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500739 ACPI_EXCEPTION((AE_INFO, status,
Bob Moore7505da42014-04-30 10:06:15 +0800740 "Unable to queue handler for GPE %02X - event disabled",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500741 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 break;
744
745 default:
Lin Ming0f849d22010-04-06 14:52:37 +0800746 /*
747 * No handler or method to run!
748 * 03/2010: This case should no longer be possible. We will not allow
749 * a GPE to be enabled if it has no handler or method.
750 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500751 ACPI_ERROR((AE_INFO,
Bob Moore7505da42014-04-30 10:06:15 +0800752 "No handler or method for GPE %02X, disabling event",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500753 gpe_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 }
757
Bob Moorefd1af712013-03-08 09:22:23 +0000758 return_UINT32(ACPI_INTERRUPT_HANDLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Bob Moore33620c52012-02-14 18:14:27 +0800760
761#endif /* !ACPI_REDUCED_HARDWARE */