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

/*
8
 * Copyright (C) 2000 - 2013, 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")
51
#if (!ACPI_REDUCED_HARDWARE)	/* Entire module */
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
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);

L
Linus Torvalds 已提交
57 58
/*******************************************************************************
 *
59
 * FUNCTION:    acpi_ev_update_gpe_enable_mask
L
Linus Torvalds 已提交
60 61 62 63 64
 *
 * PARAMETERS:  gpe_event_info          - GPE to update
 *
 * RETURN:      Status
 *
65 66
 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
 *              runtime references to this GPE
L
Linus Torvalds 已提交
67 68 69 70
 *
 ******************************************************************************/

acpi_status
71
acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
L
Linus Torvalds 已提交
72
{
L
Len Brown 已提交
73
	struct acpi_gpe_register_info *gpe_register_info;
74
	u32 register_bit;
L
Linus Torvalds 已提交
75

76
	ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);
L
Linus Torvalds 已提交
77 78 79

	gpe_register_info = gpe_event_info->register_info;
	if (!gpe_register_info) {
L
Len Brown 已提交
80
		return_ACPI_STATUS(AE_NOT_EXIST);
L
Linus Torvalds 已提交
81
	}
82

83
	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
L
Linus Torvalds 已提交
84

85
	/* Clear the run bit up front */
86

87
	ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
L
Linus Torvalds 已提交
88

89
	/* Set the mask bit only if there are references to this GPE */
90 91

	if (gpe_event_info->runtime_count) {
L
Lv Zheng 已提交
92 93
		ACPI_SET_BIT(gpe_register_info->enable_for_run,
			     (u8)register_bit);
94
	}
L
Linus Torvalds 已提交
95

L
Len Brown 已提交
96
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
97 98
}

99 100 101 102 103 104 105 106
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_enable_gpe
 *
 * PARAMETERS:  gpe_event_info  - GPE to enable
 *
 * RETURN:      Status
 *
107
 * DESCRIPTION: Clear a GPE of stale events and enable it.
108 109
 *
 ******************************************************************************/
L
Lv Zheng 已提交
110
acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
111 112 113 114 115 116
{
	acpi_status status;

	ACPI_FUNCTION_TRACE(ev_enable_gpe);

	/*
L
Lin Ming 已提交
117 118 119 120
	 * We will only allow a GPE to be enabled if it has either an associated
	 * method (_Lxx/_Exx) or a handler, or is using the implicit notify
	 * feature. Otherwise, the GPE will be immediately disabled by
	 * acpi_ev_gpe_dispatch the first time it fires.
121
	 */
L
Lin Ming 已提交
122 123
	if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
	    ACPI_GPE_DISPATCH_NONE) {
124 125 126 127 128 129 130 131 132 133 134
		return_ACPI_STATUS(AE_NO_HANDLER);
	}

	/* Clear the GPE (of stale events) */
	status = acpi_hw_clear_gpe(gpe_event_info);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
	}

	/* Enable the requested GPE */

L
Lv Zheng 已提交
135
	status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
136 137 138
	return_ACPI_STATUS(status);
}

139

140 141
/*******************************************************************************
 *
142
 * FUNCTION:    acpi_ev_add_gpe_reference
143
 *
144
 * PARAMETERS:  gpe_event_info          - Add a reference to this GPE
145 146 147 148 149 150 151 152
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
 *              hardware-enabled.
 *
 ******************************************************************************/

L
Lv Zheng 已提交
153 154
acpi_status
acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
155 156 157
{
	acpi_status status = AE_OK;

158 159
	ACPI_FUNCTION_TRACE(ev_add_gpe_reference);

160 161 162 163 164 165
	if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
		return_ACPI_STATUS(AE_LIMIT);
	}

	gpe_event_info->runtime_count++;
	if (gpe_event_info->runtime_count == 1) {
166 167 168

		/* Enable on first reference */

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
		if (ACPI_SUCCESS(status)) {
			status = acpi_ev_enable_gpe(gpe_event_info);
		}

		if (ACPI_FAILURE(status)) {
			gpe_event_info->runtime_count--;
		}
	}

	return_ACPI_STATUS(status);
}

/*******************************************************************************
 *
184
 * FUNCTION:    acpi_ev_remove_gpe_reference
185
 *
186
 * PARAMETERS:  gpe_event_info          - Remove a reference to this GPE
187 188 189 190 191 192 193 194
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
 *              removed, the GPE is hardware-disabled.
 *
 ******************************************************************************/

L
Lv Zheng 已提交
195 196
acpi_status
acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
197 198 199
{
	acpi_status status = AE_OK;

200 201
	ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);

202 203 204 205 206 207
	if (!gpe_event_info->runtime_count) {
		return_ACPI_STATUS(AE_LIMIT);
	}

	gpe_event_info->runtime_count--;
	if (!gpe_event_info->runtime_count) {
208 209 210

		/* Disable on last reference */

211 212
		status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
		if (ACPI_SUCCESS(status)) {
L
Lv Zheng 已提交
213 214
			status =
			    acpi_hw_low_set_gpe(gpe_event_info,
215 216 217 218 219 220 221 222 223 224 225
						     ACPI_GPE_DISABLE);
		}

		if (ACPI_FAILURE(status)) {
			gpe_event_info->runtime_count++;
		}
	}

	return_ACPI_STATUS(status);
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_low_get_gpe_info
 *
 * PARAMETERS:  gpe_number          - Raw GPE number
 *              gpe_block           - A GPE info block
 *
 * RETURN:      A GPE event_info struct. NULL if not a valid GPE (The gpe_number
 *              is not within the specified GPE block)
 *
 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
 *              the low-level implementation of ev_get_gpe_event_info.
 *
 ******************************************************************************/

struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
						     struct acpi_gpe_block_info
						     *gpe_block)
{
	u32 gpe_index;

	/*
	 * Validate that the gpe_number is within the specified gpe_block.
	 * (Two steps)
	 */
	if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
		return (NULL);
	}

	gpe_index = gpe_number - gpe_block->block_base_number;
	if (gpe_index >= gpe_block->gpe_count) {
		return (NULL);
	}

	return (&gpe_block->event_info[gpe_index]);
}


L
Linus Torvalds 已提交
264 265 266 267
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_get_gpe_event_info
 *
268
 * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
L
Linus Torvalds 已提交
269 270 271 272 273 274 275 276 277 278 279 280
 *              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 已提交
281 282
struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
						       u32 gpe_number)
L
Linus Torvalds 已提交
283
{
L
Len Brown 已提交
284
	union acpi_operand_object *obj_desc;
285
	struct acpi_gpe_event_info *gpe_info;
286
	u32 i;
L
Linus Torvalds 已提交
287

L
Len Brown 已提交
288
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
289

290
	/* A NULL gpe_device means use the FADT-defined GPE block(s) */
L
Linus Torvalds 已提交
291 292

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

L
Linus Torvalds 已提交
294 295 296
		/* Examine GPE Block 0 and 1 (These blocks are permanent) */

		for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
297 298 299 300 301
			gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
							    acpi_gbl_gpe_fadt_blocks
							    [i]);
			if (gpe_info) {
				return (gpe_info);
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311
			}
		}

		/* 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
Lv Zheng 已提交
312 313
	obj_desc =
	    acpi_ns_get_attached_object((struct acpi_namespace_node *)
L
Len Brown 已提交
314
					       gpe_device);
L
Len Brown 已提交
315
	if (!obj_desc || !obj_desc->device.gpe_block) {
L
Linus Torvalds 已提交
316 317 318
		return (NULL);
	}

319 320
	return (acpi_ev_low_get_gpe_info
		(gpe_number, obj_desc->device.gpe_block));
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330 331
}

/*******************************************************************************
 *
 * 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
 *
332
 * DESCRIPTION: Detect if any GP events have occurred. This function is
L
Linus Torvalds 已提交
333 334 335 336
 *              executed at interrupt level.
 *
 ******************************************************************************/

L
Len Brown 已提交
337
u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
L
Linus Torvalds 已提交
338
{
B
Bob Moore 已提交
339 340 341
	acpi_status status;
	struct acpi_gpe_block_info *gpe_block;
	struct acpi_gpe_register_info *gpe_register_info;
L
Len Brown 已提交
342 343 344 345
	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
	u8 enabled_status_byte;
	u32 status_reg;
	u32 enable_reg;
B
Bob Moore 已提交
346
	acpi_cpu_flags flags;
347 348
	u32 i;
	u32 j;
L
Len Brown 已提交
349

B
Bob Moore 已提交
350
	ACPI_FUNCTION_NAME(ev_gpe_detect);
L
Linus Torvalds 已提交
351 352 353 354 355 356 357

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

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

B
Bob Moore 已提交
358 359
	/*
	 * We need to obtain the GPE lock for both the data structs and registers
360 361
	 * Note: Not necessary to obtain the hardware lock, since the GPE
	 * registers are owned by the gpe_lock.
B
Bob Moore 已提交
362
	 */
L
Len Brown 已提交
363
	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
B
Bob Moore 已提交
364 365 366

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

L
Linus Torvalds 已提交
367 368 369
	gpe_block = gpe_xrupt_list->gpe_block_list_head;
	while (gpe_block) {
		/*
370 371
		 * 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 已提交
372 373
		 */
		for (i = 0; i < gpe_block->register_count; i++) {
B
Bob Moore 已提交
374

L
Linus Torvalds 已提交
375 376 377 378
			/* Get the next status/enable pair */

			gpe_register_info = &gpe_block->register_info[i];

379 380 381 382 383 384
			/*
			 * Optimization: If there are no GPEs enabled within this
			 * register, we can safely ignore the entire register.
			 */
			if (!(gpe_register_info->enable_for_run |
			      gpe_register_info->enable_for_wake)) {
385 386 387 388 389 390 391 392 393 394 395 396
				ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
						  "Ignore disabled registers for GPE%02X-GPE%02X: "
						  "RunEnable=%02X, WakeEnable=%02X\n",
						  gpe_register_info->
						  base_gpe_number,
						  gpe_register_info->
						  base_gpe_number +
						  (ACPI_GPE_REGISTER_WIDTH - 1),
						  gpe_register_info->
						  enable_for_run,
						  gpe_register_info->
						  enable_for_wake));
397 398 399
				continue;
			}

L
Linus Torvalds 已提交
400 401
			/* Read the Status Register */

L
Len Brown 已提交
402
			status =
403 404
			    acpi_hw_read(&status_reg,
					 &gpe_register_info->status_address);
L
Len Brown 已提交
405
			if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
406 407 408 409 410
				goto unlock_and_exit;
			}

			/* Read the Enable Register */

L
Len Brown 已提交
411
			status =
412 413
			    acpi_hw_read(&enable_reg,
					 &gpe_register_info->enable_address);
L
Len Brown 已提交
414
			if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
415 416 417
				goto unlock_and_exit;
			}

L
Len Brown 已提交
418
			ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
419 420
					  "Read registers for GPE%02X-GPE%02X: Status=%02X, Enable=%02X, "
					  "RunEnable=%02X, WakeEnable=%02X\n",
L
Len Brown 已提交
421
					  gpe_register_info->base_gpe_number,
422 423 424 425 426
					  gpe_register_info->base_gpe_number +
					  (ACPI_GPE_REGISTER_WIDTH - 1),
					  status_reg, enable_reg,
					  gpe_register_info->enable_for_run,
					  gpe_register_info->enable_for_wake));
L
Linus Torvalds 已提交
427

R
Robert Moore 已提交
428
			/* Check if there is anything active at all in this register */
L
Linus Torvalds 已提交
429 430 431

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

L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440
				/* 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 已提交
441

L
Linus Torvalds 已提交
442 443
				/* Examine one GPE bit */

444
				if (enabled_status_byte & (1 << j)) {
L
Linus Torvalds 已提交
445 446 447 448
					/*
					 * Found an active GPE. Dispatch the event to a handler
					 * or method.
					 */
L
Len Brown 已提交
449
					int_status |=
450 451 452
					    acpi_ev_gpe_dispatch(gpe_block->
								 node,
								 &gpe_block->
453
						event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
L
Linus Torvalds 已提交
454 455 456 457 458 459 460
				}
			}
		}

		gpe_block = gpe_block->next;
	}

L
Len Brown 已提交
461
      unlock_and_exit:
L
Linus Torvalds 已提交
462

L
Len Brown 已提交
463
	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474
	return (int_status);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_asynch_execute_gpe_method
 *
 * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
 *
 * RETURN:      None
 *
B
Bob Moore 已提交
475 476 477
 * 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 已提交
478 479 480 481 482
 *              the control method itself is not executed in the context of
 *              an interrupt handler.
 *
 ******************************************************************************/

L
Len Brown 已提交
483
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
L
Linus Torvalds 已提交
484
{
485
	struct acpi_gpe_event_info *gpe_event_info = context;
L
Len Brown 已提交
486
	acpi_status status;
487
	struct acpi_gpe_event_info *local_gpe_event_info;
B
Bob Moore 已提交
488
	struct acpi_evaluate_info *info;
489
	struct acpi_gpe_notify_info *notify;
L
Linus Torvalds 已提交
490

B
Bob Moore 已提交
491
	ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
L
Linus Torvalds 已提交
492

493 494 495 496 497 498 499 500 501
	/* Allocate a local GPE block */

	local_gpe_event_info =
	    ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_event_info));
	if (!local_gpe_event_info) {
		ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "while handling a GPE"));
		return_VOID;
	}

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

	/* Must revalidate the gpe_number/gpe_block */

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

	/*
517 518
	 * 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 已提交
519
	 */
520
	ACPI_MEMCPY(local_gpe_event_info, gpe_event_info,
L
Len Brown 已提交
521
		    sizeof(struct acpi_gpe_event_info));
L
Linus Torvalds 已提交
522

L
Len Brown 已提交
523 524
	status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
525 526 527
		return_VOID;
	}

L
Lin Ming 已提交
528 529 530 531 532 533 534 535 536 537 538
	/* Do the correct dispatch - normal method or implicit notify */

	switch (local_gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
	case ACPI_GPE_DISPATCH_NOTIFY:
		/*
		 * Implicit notify.
		 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
		 * NOTE: the request is queued for execution after this method
		 * completes. The notify handlers are NOT invoked synchronously
		 * from this thread -- because handlers may in turn run other
		 * control methods.
539 540 541
		 *
		 * June 2012: Expand implicit notify mechanism to support
		 * notifies on multiple device objects.
L
Lin Ming 已提交
542
		 */
543 544 545 546 547 548 549
		notify = local_gpe_event_info->dispatch.notify_list;
		while (ACPI_SUCCESS(status) && notify) {
			status =
			    acpi_ev_queue_notify_request(notify->device_node,
							 ACPI_NOTIFY_DEVICE_WAKE);

			notify = notify->next;
550 551
		}

L
Lin Ming 已提交
552 553 554
		break;

	case ACPI_GPE_DISPATCH_METHOD:
L
Linus Torvalds 已提交
555

B
Bob Moore 已提交
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 {
			/*
L
Lv Zheng 已提交
563 564
			 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
			 * _Lxx/_Exx control method that corresponds to this GPE
B
Bob Moore 已提交
565 566
			 */
			info->prefix_node =
567
			    local_gpe_event_info->dispatch.method_node;
B
Bob Moore 已提交
568 569 570 571 572 573
			info->flags = ACPI_IGNORE_RETURN_VALUE;

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

L
Len Brown 已提交
574
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
575
			ACPI_EXCEPTION((AE_INFO, status,
B
Bob Moore 已提交
576
					"while evaluating GPE method [%4.4s]",
B
Bob Moore 已提交
577
					acpi_ut_get_node_name
578
					(local_gpe_event_info->dispatch.
B
Bob Moore 已提交
579
					 method_node)));
L
Linus Torvalds 已提交
580
		}
L
Lin Ming 已提交
581 582 583
		break;

	default:
584 585

		return_VOID;	/* Should never happen */
L
Linus Torvalds 已提交
586
	}
587

588
	/* Defer enabling of GPE until all notify handlers are done */
589 590 591 592 593 594 595

	status = acpi_os_execute(OSL_NOTIFY_HANDLER,
				 acpi_ev_asynch_enable_gpe,
				 local_gpe_event_info);
	if (ACPI_FAILURE(status)) {
		ACPI_FREE(local_gpe_event_info);
	}
596 597
	return_VOID;
}
L
Linus Torvalds 已提交
598

L
Lin Ming 已提交
599

600 601 602 603 604 605 606 607 608 609
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_asynch_enable_gpe
 *
 * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
 *              Callback from acpi_os_execute
 *
 * RETURN:      None
 *
 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
L
Lin Ming 已提交
610
 *              complete (i.e., finish execution of Notify)
611 612 613 614
 *
 ******************************************************************************/

static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
615 616
{
	struct acpi_gpe_event_info *gpe_event_info = context;
L
Lin Ming 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639

	(void)acpi_ev_finish_gpe(gpe_event_info);

	ACPI_FREE(gpe_event_info);
	return;
}


/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_finish_gpe
 *
 * PARAMETERS:  gpe_event_info      - Info for this GPE
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
 *              of a GPE method or a synchronous or asynchronous GPE handler.
 *
 ******************************************************************************/

acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)
{
640
	acpi_status status;
641

642
	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
643
	    ACPI_GPE_LEVEL_TRIGGERED) {
L
Linus Torvalds 已提交
644
		/*
L
Lin Ming 已提交
645 646
		 * GPE is level-triggered, we clear the GPE status bit after
		 * handling the event.
L
Linus Torvalds 已提交
647
		 */
648
		status = acpi_hw_clear_gpe(gpe_event_info);
L
Len Brown 已提交
649
		if (ACPI_FAILURE(status)) {
L
Lin Ming 已提交
650
			return (status);
L
Linus Torvalds 已提交
651 652 653
		}
	}

B
Bob Moore 已提交
654
	/*
L
Lin Ming 已提交
655 656 657
	 * Enable this GPE, conditionally. This means that the GPE will
	 * only be physically enabled if the enable_for_run bit is set
	 * in the event_info.
B
Bob Moore 已提交
658
	 */
659
	(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
L
Lin Ming 已提交
660
	return (AE_OK);
L
Linus Torvalds 已提交
661 662
}

L
Lin Ming 已提交
663

L
Linus Torvalds 已提交
664 665 666 667
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_gpe_dispatch
 *
668 669
 * PARAMETERS:  gpe_device      - Device node. NULL for GPE0/GPE1
 *              gpe_event_info  - Info for this GPE
L
Linus Torvalds 已提交
670 671 672 673 674 675 676 677 678 679 680 681
 *              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
682 683
acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
		    struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
L
Linus Torvalds 已提交
684
{
L
Len Brown 已提交
685
	acpi_status status;
L
Lin Ming 已提交
686
	u32 return_value;
L
Linus Torvalds 已提交
687

B
Bob Moore 已提交
688
	ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
L
Linus Torvalds 已提交
689

L
Lin Ming 已提交
690 691 692 693 694 695 696 697
	/* Invoke global event handler if present */

	acpi_gpe_count++;
	if (acpi_gbl_global_event_handler) {
		acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE, gpe_device,
					      gpe_number,
					      acpi_gbl_global_event_handler_context);
	}
698

L
Linus Torvalds 已提交
699
	/*
700
	 * If edge-triggered, clear the GPE status bit now. Note that
L
Linus Torvalds 已提交
701 702
	 * level-triggered events are cleared after the GPE is serviced.
	 */
R
Robert Moore 已提交
703
	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
704 705 706
	    ACPI_GPE_EDGE_TRIGGERED) {
		status = acpi_hw_clear_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
707
			ACPI_EXCEPTION((AE_INFO, status,
708
					"Unable to clear GPE%02X", gpe_number));
709
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Linus Torvalds 已提交
710 711 712 713
		}
	}

	/*
L
Lin Ming 已提交
714 715 716 717 718 719 720 721 722 723 724 725
	 * Always disable the GPE so that it does not keep firing before
	 * any asynchronous activity completes (either from the execution
	 * of a GPE method or an asynchronous GPE handler.)
	 *
	 * If there is no handler or method to run, just disable the
	 * GPE and leave it disabled permanently to prevent further such
	 * pointless events from firing.
	 */
	status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
	if (ACPI_FAILURE(status)) {
		ACPI_EXCEPTION((AE_INFO, status,
				"Unable to disable GPE%02X", gpe_number));
726
		return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Lin Ming 已提交
727 728 729 730 731 732 733 734
	}

	/*
	 * 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, leave the GPE
	 * disabled.
L
Linus Torvalds 已提交
735 736 737 738
	 */
	switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
	case ACPI_GPE_DISPATCH_HANDLER:

L
Lin Ming 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751
		/* Invoke the installed handler (at interrupt level) */

		return_value =
		    gpe_event_info->dispatch.handler->address(gpe_device,
							      gpe_number,
							      gpe_event_info->
							      dispatch.handler->
							      context);

		/* If requested, clear (if level-triggered) and reenable the GPE */

		if (return_value & ACPI_REENABLE_GPE) {
			(void)acpi_ev_finish_gpe(gpe_event_info);
L
Linus Torvalds 已提交
752 753 754 755
		}
		break;

	case ACPI_GPE_DISPATCH_METHOD:
L
Lin Ming 已提交
756
	case ACPI_GPE_DISPATCH_NOTIFY:
L
Linus Torvalds 已提交
757 758 759 760
		/*
		 * Execute the method associated with the GPE
		 * NOTE: Level-triggered GPEs are cleared after the method completes.
		 */
B
Bob Moore 已提交
761 762 763
		status = acpi_os_execute(OSL_GPE_HANDLER,
					 acpi_ev_asynch_execute_gpe_method,
					 gpe_event_info);
L
Len Brown 已提交
764
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
765
			ACPI_EXCEPTION((AE_INFO, status,
L
Lv Zheng 已提交
766
					"Unable to queue handler for GPE%02X - event disabled",
B
Bob Moore 已提交
767
					gpe_number));
L
Linus Torvalds 已提交
768 769 770 771
		}
		break;

	default:
772 773 774 775 776
		/*
		 * No handler or method to run!
		 * 03/2010: This case should no longer be possible. We will not allow
		 * a GPE to be enabled if it has no handler or method.
		 */
B
Bob Moore 已提交
777
		ACPI_ERROR((AE_INFO,
778
			    "No handler or method for GPE%02X, disabling event",
B
Bob Moore 已提交
779
			    gpe_number));
L
Linus Torvalds 已提交
780 781 782 783

		break;
	}

784
	return_UINT32(ACPI_INTERRUPT_HANDLED);
L
Linus Torvalds 已提交
785
}
786 787

#endif				/* !ACPI_REDUCED_HARDWARE */