evgpe.c 20.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/******************************************************************************
 *
 * Module Name: evgpe - General Purpose Event handling and dispatch
 *
 *****************************************************************************/

/*
L
Len Brown 已提交
8
 * Copyright (C) 2000 - 2008, Intel Corp.
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
L
Len Brown 已提交
45 46 47
#include "accommon.h"
#include "acevents.h"
#include "acnamesp.h"
L
Linus Torvalds 已提交
48 49

#define _COMPONENT          ACPI_EVENTS
L
Len Brown 已提交
50
ACPI_MODULE_NAME("evgpe")
L
Linus Torvalds 已提交
51

R
Robert Moore 已提交
52
/* Local prototypes */
L
Len Brown 已提交
53
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_set_gpe_type
 *
 * PARAMETERS:  gpe_event_info          - GPE to set
 *              Type                    - New type
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Sets the new type for the GPE (wake, run, or wake/run)
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
69
acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type)
L
Linus Torvalds 已提交
70
{
L
Len Brown 已提交
71
	acpi_status status;
L
Linus Torvalds 已提交
72

B
Bob Moore 已提交
73
	ACPI_FUNCTION_TRACE(ev_set_gpe_type);
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83

	/* Validate type and update register enable masks */

	switch (type) {
	case ACPI_GPE_TYPE_WAKE:
	case ACPI_GPE_TYPE_RUNTIME:
	case ACPI_GPE_TYPE_WAKE_RUN:
		break;

	default:
L
Len Brown 已提交
84
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
85 86 87 88
	}

	/* Disable the GPE if currently enabled */

L
Len Brown 已提交
89
	status = acpi_ev_disable_gpe(gpe_event_info);
L
Linus Torvalds 已提交
90

91
	/* Clear the type bits and insert the new Type */
L
Linus Torvalds 已提交
92

93 94
	gpe_event_info->flags &= ~ACPI_GPE_TYPE_MASK;
	gpe_event_info->flags |= type;
L
Len Brown 已提交
95
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_update_gpe_enable_masks
 *
 * PARAMETERS:  gpe_event_info          - GPE to update
 *              Type                    - What to do: ACPI_GPE_DISABLE or
 *                                        ACPI_GPE_ENABLE
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Updates GPE register enable masks based on the GPE type
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
113 114
acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
				u8 type)
L
Linus Torvalds 已提交
115
{
L
Len Brown 已提交
116 117
	struct acpi_gpe_register_info *gpe_register_info;
	u8 register_bit;
L
Linus Torvalds 已提交
118

B
Bob Moore 已提交
119
	ACPI_FUNCTION_TRACE(ev_update_gpe_enable_masks);
L
Linus Torvalds 已提交
120 121 122

	gpe_register_info = gpe_event_info->register_info;
	if (!gpe_register_info) {
L
Len Brown 已提交
123
		return_ACPI_STATUS(AE_NOT_EXIST);
L
Linus Torvalds 已提交
124
	}
125

126 127 128
	register_bit = (u8)
	    (1 <<
	     (gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));
L
Linus Torvalds 已提交
129

130
	/* 1) Disable case. Simply clear all enable bits */
L
Linus Torvalds 已提交
131 132

	if (type == ACPI_GPE_DISABLE) {
L
Len Brown 已提交
133 134 135 136
		ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
			       register_bit);
		ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
137 138
	}

139
	/* 2) Enable case. Set/Clear the appropriate enable bits */
L
Linus Torvalds 已提交
140 141 142

	switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
	case ACPI_GPE_TYPE_WAKE:
L
Len Brown 已提交
143 144
		ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
		ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
L
Linus Torvalds 已提交
145 146 147
		break;

	case ACPI_GPE_TYPE_RUNTIME:
L
Len Brown 已提交
148 149 150
		ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
			       register_bit);
		ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
L
Linus Torvalds 已提交
151 152 153
		break;

	case ACPI_GPE_TYPE_WAKE_RUN:
L
Len Brown 已提交
154 155
		ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
		ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
L
Linus Torvalds 已提交
156 157 158
		break;

	default:
L
Len Brown 已提交
159
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
160 161
	}

L
Len Brown 已提交
162
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_enable_gpe
 *
 * PARAMETERS:  gpe_event_info          - GPE to enable
 *              write_to_hardware       - Enable now, or just mark data structs
 *                                        (WAKE GPEs should be deferred)
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Enable a GPE based on the GPE type
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
180 181
acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
		   u8 write_to_hardware)
L
Linus Torvalds 已提交
182
{
L
Len Brown 已提交
183
	acpi_status status;
L
Linus Torvalds 已提交
184

B
Bob Moore 已提交
185
	ACPI_FUNCTION_TRACE(ev_enable_gpe);
L
Linus Torvalds 已提交
186 187 188

	/* Make sure HW enable masks are updated */

L
Len Brown 已提交
189 190 191 192
	status =
	    acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_ENABLE);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
193 194 195 196 197 198 199
	}

	/* Mark wake-enabled or HW enable, or both */

	switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
	case ACPI_GPE_TYPE_WAKE:

L
Len Brown 已提交
200
		ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
L
Linus Torvalds 已提交
201 202 203 204
		break;

	case ACPI_GPE_TYPE_WAKE_RUN:

L
Len Brown 已提交
205
		ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
L
Linus Torvalds 已提交
206 207 208 209 210

		/*lint -fallthrough */

	case ACPI_GPE_TYPE_RUNTIME:

L
Len Brown 已提交
211
		ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
L
Linus Torvalds 已提交
212 213

		if (write_to_hardware) {
B
Bob Moore 已提交
214

L
Linus Torvalds 已提交
215 216
			/* Clear the GPE (of stale events), then enable it */

L
Len Brown 已提交
217 218 219
			status = acpi_hw_clear_gpe(gpe_event_info);
			if (ACPI_FAILURE(status)) {
				return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
220 221 222 223
			}

			/* Enable the requested runtime GPE */

L
Len Brown 已提交
224
			status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
L
Linus Torvalds 已提交
225 226 227 228
		}
		break;

	default:
L
Len Brown 已提交
229
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
230 231
	}

L
Len Brown 已提交
232
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_disable_gpe
 *
 * PARAMETERS:  gpe_event_info          - GPE to disable
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Disable a GPE based on the GPE type
 *
 ******************************************************************************/

L
Len Brown 已提交
247
acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
L
Linus Torvalds 已提交
248
{
L
Len Brown 已提交
249
	acpi_status status;
L
Linus Torvalds 已提交
250

B
Bob Moore 已提交
251
	ACPI_FUNCTION_TRACE(ev_disable_gpe);
L
Linus Torvalds 已提交
252 253 254

	/* Make sure HW enable masks are updated */

L
Len Brown 已提交
255 256 257 258
	status =
	    acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_DISABLE);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
259 260
	}

B
Bob Moore 已提交
261
	/* Clear the appropriate enabled flags for this GPE */
L
Linus Torvalds 已提交
262 263 264

	switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
	case ACPI_GPE_TYPE_WAKE:
L
Len Brown 已提交
265
		ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
L
Linus Torvalds 已提交
266 267 268
		break;

	case ACPI_GPE_TYPE_WAKE_RUN:
L
Len Brown 已提交
269
		ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
L
Linus Torvalds 已提交
270

271
		/* fallthrough */
L
Linus Torvalds 已提交
272 273 274 275 276

	case ACPI_GPE_TYPE_RUNTIME:

		/* Disable the requested runtime GPE */

L
Len Brown 已提交
277
		ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
B
Bob Moore 已提交
278
		break;
L
Linus Torvalds 已提交
279 280

	default:
B
Bob Moore 已提交
281
		break;
L
Linus Torvalds 已提交
282 283
	}

B
Bob Moore 已提交
284 285 286 287 288 289 290 291 292 293
	/*
	 * Even if we don't know the GPE type, make sure that we always
	 * disable it. low_disable_gpe will just clear the enable bit for this
	 * GPE and write it. It will not write out the current GPE enable mask,
	 * since this may inadvertently enable GPEs too early, if a rogue GPE has
	 * come in during ACPICA initialization - possibly as a result of AML or
	 * other code that has enabled the GPE.
	 */
	status = acpi_hw_low_disable_gpe(gpe_event_info);
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
294 295 296 297 298 299
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_get_gpe_event_info
 *
300
 * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309 310 311 312
 *              gpe_number          - Raw GPE number
 *
 * RETURN:      A GPE event_info struct. NULL if not a valid GPE
 *
 * DESCRIPTION: Returns the event_info struct associated with this GPE.
 *              Validates the gpe_block and the gpe_number
 *
 *              Should be called only when the GPE lists are semaphore locked
 *              and not subject to change.
 *
 ******************************************************************************/

L
Len Brown 已提交
313 314
struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
						       u32 gpe_number)
L
Linus Torvalds 已提交
315
{
L
Len Brown 已提交
316 317
	union acpi_operand_object *obj_desc;
	struct acpi_gpe_block_info *gpe_block;
318
	u32 i;
L
Linus Torvalds 已提交
319

L
Len Brown 已提交
320
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
321 322 323 324

	/* A NULL gpe_block means use the FADT-defined GPE block(s) */

	if (!gpe_device) {
B
Bob Moore 已提交
325

L
Linus Torvalds 已提交
326 327 328 329 330
		/* Examine GPE Block 0 and 1 (These blocks are permanent) */

		for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
			gpe_block = acpi_gbl_gpe_fadt_blocks[i];
			if (gpe_block) {
L
Len Brown 已提交
331 332 333 334 335 336 337 338
				if ((gpe_number >= gpe_block->block_base_number)
				    && (gpe_number <
					gpe_block->block_base_number +
					(gpe_block->register_count * 8))) {
					return (&gpe_block->
						event_info[gpe_number -
							   gpe_block->
							   block_base_number]);
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349
				}
			}
		}

		/* The gpe_number was not in the range of either FADT GPE block */

		return (NULL);
	}

	/* A Non-NULL gpe_device means this is a GPE Block Device */

L
Len Brown 已提交
350 351
	obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
					       gpe_device);
L
Len Brown 已提交
352
	if (!obj_desc || !obj_desc->device.gpe_block) {
L
Linus Torvalds 已提交
353 354 355 356 357 358
		return (NULL);
	}

	gpe_block = obj_desc->device.gpe_block;

	if ((gpe_number >= gpe_block->block_base_number) &&
L
Len Brown 已提交
359 360 361 362
	    (gpe_number <
	     gpe_block->block_base_number + (gpe_block->register_count * 8))) {
		return (&gpe_block->
			event_info[gpe_number - gpe_block->block_base_number]);
L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376
	}

	return (NULL);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_gpe_detect
 *
 * PARAMETERS:  gpe_xrupt_list      - Interrupt block for this interrupt.
 *                                    Can have multiple GPE blocks attached.
 *
 * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
 *
377
 * DESCRIPTION: Detect if any GP events have occurred. This function is
L
Linus Torvalds 已提交
378 379 380 381
 *              executed at interrupt level.
 *
 ******************************************************************************/

L
Len Brown 已提交
382
u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
L
Linus Torvalds 已提交
383
{
B
Bob Moore 已提交
384 385 386
	acpi_status status;
	struct acpi_gpe_block_info *gpe_block;
	struct acpi_gpe_register_info *gpe_register_info;
L
Len Brown 已提交
387 388 389 390
	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
	u8 enabled_status_byte;
	u32 status_reg;
	u32 enable_reg;
B
Bob Moore 已提交
391
	acpi_cpu_flags flags;
392 393
	u32 i;
	u32 j;
L
Len Brown 已提交
394

B
Bob Moore 已提交
395
	ACPI_FUNCTION_NAME(ev_gpe_detect);
L
Linus Torvalds 已提交
396 397 398 399 400 401 402

	/* Check for the case where there are no GPEs */

	if (!gpe_xrupt_list) {
		return (int_status);
	}

B
Bob Moore 已提交
403 404
	/*
	 * We need to obtain the GPE lock for both the data structs and registers
405 406
	 * Note: Not necessary to obtain the hardware lock, since the GPE
	 * registers are owned by the gpe_lock.
B
Bob Moore 已提交
407
	 */
L
Len Brown 已提交
408
	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
B
Bob Moore 已提交
409 410 411

	/* Examine all GPE blocks attached to this interrupt level */

L
Linus Torvalds 已提交
412 413 414
	gpe_block = gpe_xrupt_list->gpe_block_list_head;
	while (gpe_block) {
		/*
415 416
		 * Read all of the 8-bit GPE status and enable registers in this GPE
		 * block, saving all of them. Find all currently active GP events.
L
Linus Torvalds 已提交
417 418
		 */
		for (i = 0; i < gpe_block->register_count; i++) {
B
Bob Moore 已提交
419

L
Linus Torvalds 已提交
420 421 422 423 424 425
			/* Get the next status/enable pair */

			gpe_register_info = &gpe_block->register_info[i];

			/* Read the Status Register */

L
Len Brown 已提交
426
			status =
427 428
			    acpi_read(&status_reg,
				      &gpe_register_info->status_address);
L
Len Brown 已提交
429
			if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
430 431 432 433 434
				goto unlock_and_exit;
			}

			/* Read the Enable Register */

L
Len Brown 已提交
435
			status =
436 437
			    acpi_read(&enable_reg,
				      &gpe_register_info->enable_address);
L
Len Brown 已提交
438
			if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
439 440 441
				goto unlock_and_exit;
			}

L
Len Brown 已提交
442 443 444 445
			ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
					  "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
					  gpe_register_info->base_gpe_number,
					  status_reg, enable_reg));
L
Linus Torvalds 已提交
446

R
Robert Moore 已提交
447
			/* Check if there is anything active at all in this register */
L
Linus Torvalds 已提交
448 449 450

			enabled_status_byte = (u8) (status_reg & enable_reg);
			if (!enabled_status_byte) {
B
Bob Moore 已提交
451

L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459
				/* No active GPEs in this register, move on */

				continue;
			}

			/* Now look at the individual GPEs in this byte register */

			for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
B
Bob Moore 已提交
460

L
Linus Torvalds 已提交
461 462
				/* Examine one GPE bit */

463
				if (enabled_status_byte & (1 << j)) {
L
Linus Torvalds 已提交
464 465 466 467
					/*
					 * Found an active GPE. Dispatch the event to a handler
					 * or method.
					 */
L
Len Brown 已提交
468 469
					int_status |=
					    acpi_ev_gpe_dispatch(&gpe_block->
470
						event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477
				}
			}
		}

		gpe_block = gpe_block->next;
	}

L
Len Brown 已提交
478
      unlock_and_exit:
L
Linus Torvalds 已提交
479

L
Len Brown 已提交
480
	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488 489 490 491
	return (int_status);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_asynch_execute_gpe_method
 *
 * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
 *
 * RETURN:      None
 *
B
Bob Moore 已提交
492 493 494
 * DESCRIPTION: Perform the actual execution of a GPE control method. This
 *              function is called from an invocation of acpi_os_execute and
 *              therefore does NOT execute at interrupt level - so that
L
Linus Torvalds 已提交
495 496 497 498
 *              the control method itself is not executed in the context of
 *              an interrupt handler.
 *
 ******************************************************************************/
499
static void acpi_ev_asynch_enable_gpe(void *context);
L
Linus Torvalds 已提交
500

L
Len Brown 已提交
501
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
L
Linus Torvalds 已提交
502
{
L
Len Brown 已提交
503 504 505
	struct acpi_gpe_event_info *gpe_event_info = (void *)context;
	acpi_status status;
	struct acpi_gpe_event_info local_gpe_event_info;
B
Bob Moore 已提交
506
	struct acpi_evaluate_info *info;
L
Linus Torvalds 已提交
507

B
Bob Moore 已提交
508
	ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
L
Linus Torvalds 已提交
509

L
Len Brown 已提交
510 511
	status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
512 513 514 515 516
		return_VOID;
	}

	/* Must revalidate the gpe_number/gpe_block */

L
Len Brown 已提交
517 518
	if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
		status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
L
Linus Torvalds 已提交
519 520 521 522 523
		return_VOID;
	}

	/* Set the GPE flags for return to enabled state */

L
Len Brown 已提交
524
	(void)acpi_ev_enable_gpe(gpe_event_info, FALSE);
L
Linus Torvalds 已提交
525 526

	/*
527 528
	 * Take a snapshot of the GPE info for this level - we copy the info to
	 * prevent a race condition with remove_handler/remove_block.
L
Linus Torvalds 已提交
529
	 */
L
Len Brown 已提交
530 531
	ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
		    sizeof(struct acpi_gpe_event_info));
L
Linus Torvalds 已提交
532

L
Len Brown 已提交
533 534
	status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
535 536 537 538
		return_VOID;
	}

	/*
539 540
	 * Must check for control method type dispatch one more time to avoid a
	 * race with ev_gpe_install_handler
L
Linus Torvalds 已提交
541
	 */
R
Robert Moore 已提交
542
	if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
L
Len Brown 已提交
543
	    ACPI_GPE_DISPATCH_METHOD) {
L
Linus Torvalds 已提交
544

B
Bob Moore 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
		/* Allocate the evaluation information block */

		info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
		if (!info) {
			status = AE_NO_MEMORY;
		} else {
			/*
			 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
			 * control method that corresponds to this GPE
			 */
			info->prefix_node =
			    local_gpe_event_info.dispatch.method_node;
			info->flags = ACPI_IGNORE_RETURN_VALUE;

			status = acpi_ns_evaluate(info);
			ACPI_FREE(info);
		}

L
Len Brown 已提交
563
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
564
			ACPI_EXCEPTION((AE_INFO, status,
B
Bob Moore 已提交
565
					"while evaluating GPE method [%4.4s]",
B
Bob Moore 已提交
566 567
					acpi_ut_get_node_name
					(local_gpe_event_info.dispatch.
B
Bob Moore 已提交
568
					 method_node)));
L
Linus Torvalds 已提交
569 570
		}
	}
571 572 573 574 575
	/* Defer enabling of GPE until all notify handlers are done */
	acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
				gpe_event_info);
	return_VOID;
}
L
Linus Torvalds 已提交
576

577 578 579 580 581
static void acpi_ev_asynch_enable_gpe(void *context)
{
	struct acpi_gpe_event_info *gpe_event_info = context;
	acpi_status status;
	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
582
	    ACPI_GPE_LEVEL_TRIGGERED) {
L
Linus Torvalds 已提交
583
		/*
584 585
		 * GPE is level-triggered, we clear the GPE status bit after handling
		 * the event.
L
Linus Torvalds 已提交
586
		 */
587
		status = acpi_hw_clear_gpe(gpe_event_info);
L
Len Brown 已提交
588
		if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
589 590 591 592 593
			return_VOID;
		}
	}

	/* Enable this GPE */
594
	(void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
L
Linus Torvalds 已提交
595 596 597 598 599 600 601
	return_VOID;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_gpe_dispatch
 *
R
Robert Moore 已提交
602
 * PARAMETERS:  gpe_event_info  - Info for this GPE
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610 611 612 613 614
 *              gpe_number      - Number relative to the parent GPE block
 *
 * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
 *
 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
 *              or method (e.g. _Lxx/_Exx) handler.
 *
 *              This function executes at interrupt level.
 *
 ******************************************************************************/

u32
L
Len Brown 已提交
615
acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
L
Linus Torvalds 已提交
616
{
L
Len Brown 已提交
617
	acpi_status status;
L
Linus Torvalds 已提交
618

B
Bob Moore 已提交
619
	ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
L
Linus Torvalds 已提交
620

621
	acpi_os_gpe_count(gpe_number);
622

L
Linus Torvalds 已提交
623
	/*
624
	 * If edge-triggered, clear the GPE status bit now. Note that
L
Linus Torvalds 已提交
625 626
	 * level-triggered events are cleared after the GPE is serviced.
	 */
R
Robert Moore 已提交
627
	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
628 629 630
	    ACPI_GPE_EDGE_TRIGGERED) {
		status = acpi_hw_clear_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
631 632 633
			ACPI_EXCEPTION((AE_INFO, status,
					"Unable to clear GPE[%2X]",
					gpe_number));
B
Bob Moore 已提交
634
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
635 636 637 638
		}
	}

	/*
639 640 641 642 643
	 * Dispatch the GPE to either an installed handler, or the control method
	 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
	 * it and do not attempt to run the method. If there is neither a handler
	 * nor a method, we disable this GPE to prevent further such pointless
	 * events from firing.
L
Linus Torvalds 已提交
644 645 646 647 648 649
	 */
	switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
	case ACPI_GPE_DISPATCH_HANDLER:

		/*
		 * Invoke the installed handler (at interrupt level)
650 651
		 * Ignore return status for now.
		 * TBD: leave GPE disabled on error?
L
Linus Torvalds 已提交
652
		 */
L
Len Brown 已提交
653 654 655 656
		(void)gpe_event_info->dispatch.handler->address(gpe_event_info->
								dispatch.
								handler->
								context);
L
Linus Torvalds 已提交
657 658 659

		/* It is now safe to clear level-triggered events. */

R
Robert Moore 已提交
660
		if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
661 662 663
		    ACPI_GPE_LEVEL_TRIGGERED) {
			status = acpi_hw_clear_gpe(gpe_event_info);
			if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
664 665 666
				ACPI_EXCEPTION((AE_INFO, status,
						"Unable to clear GPE[%2X]",
						gpe_number));
B
Bob Moore 已提交
667
				return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
668 669 670 671 672 673 674
			}
		}
		break;

	case ACPI_GPE_DISPATCH_METHOD:

		/*
675 676
		 * Disable the GPE, so it doesn't keep firing before the method has a
		 * chance to run (it runs asynchronously with interrupts enabled).
L
Linus Torvalds 已提交
677
		 */
L
Len Brown 已提交
678 679
		status = acpi_ev_disable_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
680 681 682
			ACPI_EXCEPTION((AE_INFO, status,
					"Unable to disable GPE[%2X]",
					gpe_number));
B
Bob Moore 已提交
683
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
684 685 686 687 688 689
		}

		/*
		 * Execute the method associated with the GPE
		 * NOTE: Level-triggered GPEs are cleared after the method completes.
		 */
B
Bob Moore 已提交
690 691 692
		status = acpi_os_execute(OSL_GPE_HANDLER,
					 acpi_ev_asynch_execute_gpe_method,
					 gpe_event_info);
L
Len Brown 已提交
693
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
694 695 696
			ACPI_EXCEPTION((AE_INFO, status,
					"Unable to queue handler for GPE[%2X] - event disabled",
					gpe_number));
L
Linus Torvalds 已提交
697 698 699 700 701 702 703
		}
		break;

	default:

		/* No handler or method to run! */

B
Bob Moore 已提交
704 705 706
		ACPI_ERROR((AE_INFO,
			    "No handler or method for GPE[%2X], disabling event",
			    gpe_number));
L
Linus Torvalds 已提交
707 708

		/*
709
		 * Disable the GPE. The GPE will remain disabled until the ACPICA
L
Linus Torvalds 已提交
710 711
		 * Core Subsystem is restarted, or a handler is installed.
		 */
L
Len Brown 已提交
712 713
		status = acpi_ev_disable_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
714 715 716
			ACPI_EXCEPTION((AE_INFO, status,
					"Unable to disable GPE[%2X]",
					gpe_number));
B
Bob Moore 已提交
717
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
718 719 720 721
		}
		break;
	}

B
Bob Moore 已提交
722
	return_UINT32(ACPI_INTERRUPT_HANDLED);
L
Linus Torvalds 已提交
723
}