evgpe.c 21.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 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 45 46 47 48
/******************************************************************************
 *
 * Module Name: evgpe - General Purpose Event handling and dispatch
 *
 *****************************************************************************/

/*
 * Copyright (C) 2000 - 2005, R. Byron Moore
 * 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>
#include <acpi/acevents.h>
#include <acpi/acnamesp.h>

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

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

/*******************************************************************************
 *
 * 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 已提交
68
acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type)
L
Linus Torvalds 已提交
69
{
L
Len Brown 已提交
70
	acpi_status status;
L
Linus Torvalds 已提交
71

L
Len Brown 已提交
72
	ACPI_FUNCTION_TRACE("ev_set_gpe_type");
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82

	/* 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 已提交
83
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
84 85 86 87
	}

	/* Disable the GPE if currently enabled */

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

	/* Type was validated above */

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

/*******************************************************************************
 *
 * 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 已提交
112 113
acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
				u8 type)
L
Linus Torvalds 已提交
114
{
L
Len Brown 已提交
115 116
	struct acpi_gpe_register_info *gpe_register_info;
	u8 register_bit;
L
Linus Torvalds 已提交
117

L
Len Brown 已提交
118
	ACPI_FUNCTION_TRACE("ev_update_gpe_enable_masks");
L
Linus Torvalds 已提交
119 120 121

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

	/* 1) Disable case.  Simply clear all enable bits */

	if (type == ACPI_GPE_DISABLE) {
L
Len Brown 已提交
129 130 131 132
		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 已提交
133 134 135 136 137 138
	}

	/* 2) Enable case.  Set/Clear the appropriate enable bits */

	switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
	case ACPI_GPE_TYPE_WAKE:
L
Len Brown 已提交
139 140
		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 已提交
141 142 143
		break;

	case ACPI_GPE_TYPE_RUNTIME:
L
Len Brown 已提交
144 145 146
		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 已提交
147 148 149
		break;

	case ACPI_GPE_TYPE_WAKE_RUN:
L
Len Brown 已提交
150 151
		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 已提交
152 153 154
		break;

	default:
L
Len Brown 已提交
155
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
156 157
	}

L
Len Brown 已提交
158
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
}

/*******************************************************************************
 *
 * 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 已提交
176 177
acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
		   u8 write_to_hardware)
L
Linus Torvalds 已提交
178
{
L
Len Brown 已提交
179
	acpi_status status;
L
Linus Torvalds 已提交
180

L
Len Brown 已提交
181
	ACPI_FUNCTION_TRACE("ev_enable_gpe");
L
Linus Torvalds 已提交
182 183 184

	/* Make sure HW enable masks are updated */

L
Len Brown 已提交
185 186 187 188
	status =
	    acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_ENABLE);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
189 190 191 192 193 194 195
	}

	/* 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 已提交
196
		ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
L
Linus Torvalds 已提交
197 198 199 200
		break;

	case ACPI_GPE_TYPE_WAKE_RUN:

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

		/*lint -fallthrough */

	case ACPI_GPE_TYPE_RUNTIME:

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

		if (write_to_hardware) {
			/* Clear the GPE (of stale events), then enable it */

L
Len Brown 已提交
212 213 214
			status = acpi_hw_clear_gpe(gpe_event_info);
			if (ACPI_FAILURE(status)) {
				return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
215 216 217 218
			}

			/* Enable the requested runtime GPE */

L
Len Brown 已提交
219
			status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
L
Linus Torvalds 已提交
220 221 222 223
		}
		break;

	default:
L
Len Brown 已提交
224
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
225 226
	}

L
Len Brown 已提交
227
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

/*******************************************************************************
 *
 * 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 已提交
242
acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
L
Linus Torvalds 已提交
243
{
L
Len Brown 已提交
244
	acpi_status status;
L
Linus Torvalds 已提交
245

L
Len Brown 已提交
246
	ACPI_FUNCTION_TRACE("ev_disable_gpe");
L
Linus Torvalds 已提交
247 248

	if (!(gpe_event_info->flags & ACPI_GPE_ENABLE_MASK)) {
L
Len Brown 已提交
249
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
250 251 252 253
	}

	/* Make sure HW enable masks are updated */

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

	/* Mark wake-disabled or HW disable, or both */

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

	case ACPI_GPE_TYPE_WAKE_RUN:
L
Len Brown 已提交
268
		ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
L
Linus Torvalds 已提交
269 270 271 272 273 274 275

		/*lint -fallthrough */

	case ACPI_GPE_TYPE_RUNTIME:

		/* Disable the requested runtime GPE */

L
Len Brown 已提交
276 277
		ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
		status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
L
Linus Torvalds 已提交
278 279 280
		break;

	default:
L
Len Brown 已提交
281
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
282 283
	}

L
Len Brown 已提交
284
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_get_gpe_event_info
 *
 * PARAMETERS:  gpe_device          - Device node.  NULL for GPE0/GPE1
 *              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 已提交
304 305
struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
						       u32 gpe_number)
L
Linus Torvalds 已提交
306
{
L
Len Brown 已提交
307 308 309
	union acpi_operand_object *obj_desc;
	struct acpi_gpe_block_info *gpe_block;
	acpi_native_uint i;
L
Linus Torvalds 已提交
310

L
Len Brown 已提交
311
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320

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

	if (!gpe_device) {
		/* 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 已提交
321 322 323 324 325 326 327 328
				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 已提交
329 330 331 332 333 334 335 336 337 338 339
				}
			}
		}

		/* 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 已提交
340 341 342 343
	obj_desc =
	    acpi_ns_get_attached_object((struct acpi_namespace_node *)
					gpe_device);
	if (!obj_desc || !obj_desc->device.gpe_block) {
L
Linus Torvalds 已提交
344 345 346 347 348 349
		return (NULL);
	}

	gpe_block = obj_desc->device.gpe_block;

	if ((gpe_number >= gpe_block->block_base_number) &&
L
Len Brown 已提交
350 351 352 353
	    (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 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	}

	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
 *
 * DESCRIPTION: Detect if any GP events have occurred.  This function is
 *              executed at interrupt level.
 *
 ******************************************************************************/

L
Len Brown 已提交
373
u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
L
Linus Torvalds 已提交
374
{
L
Len Brown 已提交
375 376 377 378 379 380 381 382 383 384 385 386
	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
	u8 enabled_status_byte;
	struct acpi_gpe_register_info *gpe_register_info;
	u32 status_reg;
	u32 enable_reg;
	u32 flags;
	acpi_status status;
	struct acpi_gpe_block_info *gpe_block;
	acpi_native_uint i;
	acpi_native_uint j;

	ACPI_FUNCTION_NAME("ev_gpe_detect");
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395

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

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

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

L
Len Brown 已提交
396
	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410
	gpe_block = gpe_xrupt_list->gpe_block_list_head;
	while (gpe_block) {
		/*
		 * 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.
		 */
		for (i = 0; i < gpe_block->register_count; i++) {
			/* Get the next status/enable pair */

			gpe_register_info = &gpe_block->register_info[i];

			/* Read the Status Register */

L
Len Brown 已提交
411 412 413 414 415 416
			status =
			    acpi_hw_low_level_read(ACPI_GPE_REGISTER_WIDTH,
						   &status_reg,
						   &gpe_register_info->
						   status_address);
			if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
417 418 419 420 421
				goto unlock_and_exit;
			}

			/* Read the Enable Register */

L
Len Brown 已提交
422 423 424 425 426 427
			status =
			    acpi_hw_low_level_read(ACPI_GPE_REGISTER_WIDTH,
						   &enable_reg,
						   &gpe_register_info->
						   enable_address);
			if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
428 429 430
				goto unlock_and_exit;
			}

L
Len Brown 已提交
431 432 433 434
			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 已提交
435

R
Robert Moore 已提交
436
			/* Check if there is anything active at all in this register */
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449

			enabled_status_byte = (u8) (status_reg & enable_reg);
			if (!enabled_status_byte) {
				/* 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++) {
				/* Examine one GPE bit */

L
Len Brown 已提交
450 451
				if (enabled_status_byte &
				    acpi_gbl_decode_to8bit[j]) {
L
Linus Torvalds 已提交
452 453 454 455
					/*
					 * Found an active GPE. Dispatch the event to a handler
					 * or method.
					 */
L
Len Brown 已提交
456 457 458 459 460 461 462 463 464
					int_status |=
					    acpi_ev_gpe_dispatch(&gpe_block->
								 event_info[(i *
									     ACPI_GPE_REGISTER_WIDTH)
									    +
									    j],
								 (u32) j +
								 gpe_register_info->
								 base_gpe_number);
L
Linus Torvalds 已提交
465 466 467 468 469 470 471
				}
			}
		}

		gpe_block = gpe_block->next;
	}

L
Len Brown 已提交
472
      unlock_and_exit:
L
Linus Torvalds 已提交
473

L
Len Brown 已提交
474
	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	return (int_status);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_asynch_execute_gpe_method
 *
 * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
 *
 * RETURN:      None
 *
 * DESCRIPTION: Perform the actual execution of a GPE control method.  This
 *              function is called from an invocation of acpi_os_queue_for_execution
 *              (and therefore does NOT execute at interrupt level) so that
 *              the control method itself is not executed in the context of
 *              an interrupt handler.
 *
 ******************************************************************************/

L
Len Brown 已提交
494
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
L
Linus Torvalds 已提交
495
{
L
Len Brown 已提交
496 497 498 499 500
	struct acpi_gpe_event_info *gpe_event_info = (void *)context;
	u32 gpe_number = 0;
	acpi_status status;
	struct acpi_gpe_event_info local_gpe_event_info;
	struct acpi_parameter_info info;
L
Linus Torvalds 已提交
501

L
Len Brown 已提交
502
	ACPI_FUNCTION_TRACE("ev_asynch_execute_gpe_method");
L
Linus Torvalds 已提交
503

L
Len Brown 已提交
504 505
	status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
506 507 508 509 510
		return_VOID;
	}

	/* Must revalidate the gpe_number/gpe_block */

L
Len Brown 已提交
511 512
	if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
		status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
L
Linus Torvalds 已提交
513 514 515 516 517
		return_VOID;
	}

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

L
Len Brown 已提交
518
	(void)acpi_ev_enable_gpe(gpe_event_info, FALSE);
L
Linus Torvalds 已提交
519 520 521 522 523

	/*
	 * 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
Len Brown 已提交
524 525
	ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
		    sizeof(struct acpi_gpe_event_info));
L
Linus Torvalds 已提交
526

L
Len Brown 已提交
527 528
	status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
529 530 531 532 533 534 535
		return_VOID;
	}

	/*
	 * Must check for control method type dispatch one more
	 * time to avoid race with ev_gpe_install_handler
	 */
R
Robert Moore 已提交
536
	if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
L
Len Brown 已提交
537
	    ACPI_GPE_DISPATCH_METHOD) {
L
Linus Torvalds 已提交
538 539 540 541 542
		/*
		 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
		 * control method that corresponds to this GPE
		 */
		info.node = local_gpe_event_info.dispatch.method_node;
L
Len Brown 已提交
543 544
		info.parameters =
		    ACPI_CAST_PTR(union acpi_operand_object *, gpe_event_info);
L
Linus Torvalds 已提交
545 546
		info.parameter_type = ACPI_PARAM_GPE;

L
Len Brown 已提交
547 548 549
		status = acpi_ns_evaluate_by_handle(&info);
		if (ACPI_FAILURE(status)) {
			ACPI_REPORT_ERROR(("%s while evaluating method [%4.4s] for GPE[%2X]\n", acpi_format_exception(status), acpi_ut_get_node_name(local_gpe_event_info.dispatch.method_node), gpe_number));
L
Linus Torvalds 已提交
550 551 552
		}
	}

R
Robert Moore 已提交
553
	if ((local_gpe_event_info.flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
554
	    ACPI_GPE_LEVEL_TRIGGERED) {
L
Linus Torvalds 已提交
555 556 557 558
		/*
		 * GPE is level-triggered, we clear the GPE status bit after
		 * handling the event.
		 */
L
Len Brown 已提交
559 560
		status = acpi_hw_clear_gpe(&local_gpe_event_info);
		if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
561 562 563 564 565 566
			return_VOID;
		}
	}

	/* Enable this GPE */

L
Len Brown 已提交
567
	(void)acpi_hw_write_gpe_enable_reg(&local_gpe_event_info);
L
Linus Torvalds 已提交
568 569 570 571 572 573 574
	return_VOID;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_gpe_dispatch
 *
R
Robert Moore 已提交
575
 * PARAMETERS:  gpe_event_info  - Info for this GPE
L
Linus Torvalds 已提交
576 577 578 579 580 581 582 583 584 585 586 587
 *              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 已提交
588
acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
L
Linus Torvalds 已提交
589
{
L
Len Brown 已提交
590
	acpi_status status;
L
Linus Torvalds 已提交
591

L
Len Brown 已提交
592
	ACPI_FUNCTION_TRACE("ev_gpe_dispatch");
L
Linus Torvalds 已提交
593 594 595 596 597

	/*
	 * If edge-triggered, clear the GPE status bit now.  Note that
	 * level-triggered events are cleared after the GPE is serviced.
	 */
R
Robert Moore 已提交
598
	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
599 600 601 602
	    ACPI_GPE_EDGE_TRIGGERED) {
		status = acpi_hw_clear_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
			ACPI_REPORT_ERROR(("acpi_ev_gpe_dispatch: %s, Unable to clear GPE[%2X]\n", acpi_format_exception(status), gpe_number));
B
Bob Moore 已提交
603
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
604 605 606 607 608 609
		}
	}

	/* Save current system state */

	if (acpi_gbl_system_awake_and_running) {
L
Len Brown 已提交
610 611 612
		ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_SYSTEM_RUNNING);
	} else {
		ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_SYSTEM_RUNNING);
L
Linus Torvalds 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
	}

	/*
	 * 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 the level to
	 * prevent further events from coming in here.
	 */
	switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
	case ACPI_GPE_DISPATCH_HANDLER:

		/*
		 * Invoke the installed handler (at interrupt level)
		 * Ignore return status for now.  TBD: leave GPE disabled on error?
		 */
L
Len Brown 已提交
629 630 631 632
		(void)gpe_event_info->dispatch.handler->address(gpe_event_info->
								dispatch.
								handler->
								context);
L
Linus Torvalds 已提交
633 634 635

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

R
Robert Moore 已提交
636
		if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
637 638 639 640
		    ACPI_GPE_LEVEL_TRIGGERED) {
			status = acpi_hw_clear_gpe(gpe_event_info);
			if (ACPI_FAILURE(status)) {
				ACPI_REPORT_ERROR(("acpi_ev_gpe_dispatch: %s, Unable to clear GPE[%2X]\n", acpi_format_exception(status), gpe_number));
B
Bob Moore 已提交
641
				return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
642 643 644 645 646 647 648 649 650 651
			}
		}
		break;

	case ACPI_GPE_DISPATCH_METHOD:

		/*
		 * Disable GPE, so it doesn't keep firing before the method has a
		 * chance to run.
		 */
L
Len Brown 已提交
652 653 654
		status = acpi_ev_disable_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
			ACPI_REPORT_ERROR(("acpi_ev_gpe_dispatch: %s, Unable to disable GPE[%2X]\n", acpi_format_exception(status), gpe_number));
B
Bob Moore 已提交
655
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
656 657 658 659 660 661
		}

		/*
		 * Execute the method associated with the GPE
		 * NOTE: Level-triggered GPEs are cleared after the method completes.
		 */
L
Len Brown 已提交
662 663 664 665 666
		status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
						     acpi_ev_asynch_execute_gpe_method,
						     gpe_event_info);
		if (ACPI_FAILURE(status)) {
			ACPI_REPORT_ERROR(("acpi_ev_gpe_dispatch: %s, Unable to queue handler for GPE[%2X] - event disabled\n", acpi_format_exception(status), gpe_number));
L
Linus Torvalds 已提交
667 668 669 670 671 672 673
		}
		break;

	default:

		/* No handler or method to run! */

L
Len Brown 已提交
674
		ACPI_REPORT_ERROR(("acpi_ev_gpe_dispatch: No handler or method for GPE[%2X], disabling event\n", gpe_number));
L
Linus Torvalds 已提交
675 676 677 678 679

		/*
		 * Disable the GPE.  The GPE will remain disabled until the ACPI
		 * Core Subsystem is restarted, or a handler is installed.
		 */
L
Len Brown 已提交
680 681 682
		status = acpi_ev_disable_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
			ACPI_REPORT_ERROR(("acpi_ev_gpe_dispatch: %s, Unable to disable GPE[%2X]\n", acpi_format_exception(status), gpe_number));
B
Bob Moore 已提交
683
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
684 685 686 687
		}
		break;
	}

B
Bob Moore 已提交
688
	return_UINT32(ACPI_INTERRUPT_HANDLED);
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
}

#ifdef ACPI_GPE_NOTIFY_CHECK
/*******************************************************************************
 * TBD: NOT USED, PROTOTYPE ONLY AND WILL PROBABLY BE REMOVED
 *
 * FUNCTION:    acpi_ev_check_for_wake_only_gpe
 *
 * PARAMETERS:  gpe_event_info  - info for this GPE
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Determine if a a GPE is "wake-only".
 *
 *              Called from Notify() code in interpreter when a "device_wake"
 *              Notify comes in.
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
709
acpi_ev_check_for_wake_only_gpe(struct acpi_gpe_event_info *gpe_event_info)
L
Linus Torvalds 已提交
710
{
L
Len Brown 已提交
711
	acpi_status status;
L
Linus Torvalds 已提交
712

L
Len Brown 已提交
713
	ACPI_FUNCTION_TRACE("ev_check_for_wake_only_gpe");
L
Linus Torvalds 已提交
714

L
Len Brown 已提交
715 716
	if ((gpe_event_info) &&	/* Only >0 for _Lxx/_Exx */
	    ((gpe_event_info->flags & ACPI_GPE_SYSTEM_MASK) == ACPI_GPE_SYSTEM_RUNNING)) {	/* System state at GPE time */
L
Linus Torvalds 已提交
717 718
		/* This must be a wake-only GPE, disable it */

L
Len Brown 已提交
719
		status = acpi_ev_disable_gpe(gpe_event_info);
L
Linus Torvalds 已提交
720 721 722

		/* Set GPE to wake-only.  Do not change wake disabled/enabled status */

L
Len Brown 已提交
723
		acpi_ev_set_gpe_type(gpe_event_info, ACPI_GPE_TYPE_WAKE);
L
Linus Torvalds 已提交
724

L
Len Brown 已提交
725
		ACPI_REPORT_INFO(("GPE %p was updated from wake/run to wake-only\n", gpe_event_info));
L
Linus Torvalds 已提交
726 727 728

		/* This was a wake-only GPE */

L
Len Brown 已提交
729
		return_ACPI_STATUS(AE_WAKE_ONLY_GPE);
L
Linus Torvalds 已提交
730 731
	}

L
Len Brown 已提交
732
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
733 734
}
#endif