evgpe.c 23.8 KB
Newer Older
E
Erik Schmauss 已提交
1
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5
/******************************************************************************
 *
 * Module Name: evgpe - General Purpose Event handling and dispatch
 *
6
 * Copyright (C) 2000 - 2019, Intel Corp.
L
Linus Torvalds 已提交
7
 *
E
Erik Schmauss 已提交
8
 *****************************************************************************/
L
Linus Torvalds 已提交
9 10

#include <acpi/acpi.h>
L
Len Brown 已提交
11 12 13
#include "accommon.h"
#include "acevents.h"
#include "acnamesp.h"
L
Linus Torvalds 已提交
14 15

#define _COMPONENT          ACPI_EVENTS
L
Len Brown 已提交
16
ACPI_MODULE_NAME("evgpe")
17
#if (!ACPI_REDUCED_HARDWARE)	/* Entire module */
R
Robert Moore 已提交
18
/* Local prototypes */
L
Len Brown 已提交
19
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
L
Linus Torvalds 已提交
20

21 22
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);

L
Linus Torvalds 已提交
23 24
/*******************************************************************************
 *
25
 * FUNCTION:    acpi_ev_update_gpe_enable_mask
L
Linus Torvalds 已提交
26 27 28 29 30
 *
 * PARAMETERS:  gpe_event_info          - GPE to update
 *
 * RETURN:      Status
 *
31 32
 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
 *              runtime references to this GPE
L
Linus Torvalds 已提交
33 34 35 36
 *
 ******************************************************************************/

acpi_status
37
acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
L
Linus Torvalds 已提交
38
{
L
Len Brown 已提交
39
	struct acpi_gpe_register_info *gpe_register_info;
40
	u32 register_bit;
L
Linus Torvalds 已提交
41

42
	ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);
L
Linus Torvalds 已提交
43 44 45

	gpe_register_info = gpe_event_info->register_info;
	if (!gpe_register_info) {
L
Len Brown 已提交
46
		return_ACPI_STATUS(AE_NOT_EXIST);
L
Linus Torvalds 已提交
47
	}
48

49
	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
L
Linus Torvalds 已提交
50

51
	/* Clear the run bit up front */
52

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

55
	/* Set the mask bit only if there are references to this GPE */
56 57

	if (gpe_event_info->runtime_count) {
L
Lv Zheng 已提交
58 59
		ACPI_SET_BIT(gpe_register_info->enable_for_run,
			     (u8)register_bit);
60
	}
L
Linus Torvalds 已提交
61

62
	gpe_register_info->enable_mask = gpe_register_info->enable_for_run;
L
Len Brown 已提交
63
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
64 65
}

66 67 68 69
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_enable_gpe
 *
70
 * PARAMETERS:  gpe_event_info          - GPE to enable
71 72 73
 *
 * RETURN:      Status
 *
74
 * DESCRIPTION: Enable a GPE.
75 76
 *
 ******************************************************************************/
77

L
Lv Zheng 已提交
78
acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
79 80 81 82 83
{
	acpi_status status;

	ACPI_FUNCTION_TRACE(ev_enable_gpe);

84
	/* Enable the requested GPE */
85

86
	status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
87 88 89
	return_ACPI_STATUS(status);
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_mask_gpe
 *
 * PARAMETERS:  gpe_event_info          - GPE to be blocked/unblocked
 *              is_masked               - Whether the GPE is masked or not
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Unconditionally mask/unmask a GPE during runtime.
 *
 ******************************************************************************/

acpi_status
acpi_ev_mask_gpe(struct acpi_gpe_event_info *gpe_event_info, u8 is_masked)
{
	struct acpi_gpe_register_info *gpe_register_info;
	u32 register_bit;

	ACPI_FUNCTION_TRACE(ev_mask_gpe);

	gpe_register_info = gpe_event_info->register_info;
	if (!gpe_register_info) {
		return_ACPI_STATUS(AE_NOT_EXIST);
	}

	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);

	/* Perform the action */

	if (is_masked) {
		if (register_bit & gpe_register_info->mask_for_run) {
			return_ACPI_STATUS(AE_BAD_PARAMETER);
		}

		(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
		ACPI_SET_BIT(gpe_register_info->mask_for_run, (u8)register_bit);
	} else {
		if (!(register_bit & gpe_register_info->mask_for_run)) {
			return_ACPI_STATUS(AE_BAD_PARAMETER);
		}

		ACPI_CLEAR_BIT(gpe_register_info->mask_for_run,
			       (u8)register_bit);
		if (gpe_event_info->runtime_count
		    && !gpe_event_info->disable_for_dispatch) {
			(void)acpi_hw_low_set_gpe(gpe_event_info,
						  ACPI_GPE_ENABLE);
		}
	}

	return_ACPI_STATUS(AE_OK);
}

144 145
/*******************************************************************************
 *
146
 * FUNCTION:    acpi_ev_add_gpe_reference
147
 *
148
 * PARAMETERS:  gpe_event_info          - Add a reference to this GPE
149
 *              clear_on_enable         - Clear GPE status before enabling it
150 151 152 153 154 155 156 157
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
 *              hardware-enabled.
 *
 ******************************************************************************/

L
Lv Zheng 已提交
158
acpi_status
159 160
acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info,
			  u8 clear_on_enable)
161 162 163
{
	acpi_status status = AE_OK;

164 165
	ACPI_FUNCTION_TRACE(ev_add_gpe_reference);

166 167 168 169 170 171
	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) {
172 173 174

		/* Enable on first reference */

175 176 177 178
		if (clear_on_enable) {
			(void)acpi_hw_clear_gpe(gpe_event_info);
		}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		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);
}

/*******************************************************************************
 *
194
 * FUNCTION:    acpi_ev_remove_gpe_reference
195
 *
196
 * PARAMETERS:  gpe_event_info          - Remove a reference to this GPE
197 198 199 200 201 202 203 204
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
 *              removed, the GPE is hardware-disabled.
 *
 ******************************************************************************/

L
Lv Zheng 已提交
205 206
acpi_status
acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
207 208 209
{
	acpi_status status = AE_OK;

210 211
	ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);

212 213 214 215 216 217
	if (!gpe_event_info->runtime_count) {
		return_ACPI_STATUS(AE_LIMIT);
	}

	gpe_event_info->runtime_count--;
	if (!gpe_event_info->runtime_count) {
218 219 220

		/* Disable on last reference */

221 222
		status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
		if (ACPI_SUCCESS(status)) {
L
Lv Zheng 已提交
223 224
			status =
			    acpi_hw_low_set_gpe(gpe_event_info,
225
						ACPI_GPE_DISABLE);
226 227 228 229 230 231 232 233 234 235
		}

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

	return_ACPI_STATUS(status);
}

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

L
Len Brown 已提交
298
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
299

300
	/* A NULL gpe_device means use the FADT-defined GPE block(s) */
L
Linus Torvalds 已提交
301 302

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

L
Linus Torvalds 已提交
304 305 306
		/* Examine GPE Block 0 and 1 (These blocks are permanent) */

		for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
307 308 309 310 311
			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 已提交
312 313 314 315 316 317 318 319 320 321
			}
		}

		/* 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 已提交
322 323
	obj_desc =
	    acpi_ns_get_attached_object((struct acpi_namespace_node *)
L
Len Brown 已提交
324
					       gpe_device);
L
Len Brown 已提交
325
	if (!obj_desc || !obj_desc->device.gpe_block) {
L
Linus Torvalds 已提交
326 327 328
		return (NULL);
	}

329 330
	return (acpi_ev_low_get_gpe_info
		(gpe_number, obj_desc->device.gpe_block));
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341
}

/*******************************************************************************
 *
 * 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
 *
342
 * DESCRIPTION: Detect if any GP events have occurred. This function is
L
Linus Torvalds 已提交
343 344 345 346
 *              executed at interrupt level.
 *
 ******************************************************************************/

347
u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list)
L
Linus Torvalds 已提交
348
{
B
Bob Moore 已提交
349
	struct acpi_gpe_block_info *gpe_block;
350
	struct acpi_namespace_node *gpe_device;
B
Bob Moore 已提交
351
	struct acpi_gpe_register_info *gpe_register_info;
352 353
	struct acpi_gpe_event_info *gpe_event_info;
	u32 gpe_number;
L
Len Brown 已提交
354
	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
B
Bob Moore 已提交
355
	acpi_cpu_flags flags;
356 357
	u32 i;
	u32 j;
L
Len Brown 已提交
358

B
Bob Moore 已提交
359
	ACPI_FUNCTION_NAME(ev_gpe_detect);
L
Linus Torvalds 已提交
360 361 362 363 364 365 366

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

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

B
Bob Moore 已提交
367 368
	/*
	 * We need to obtain the GPE lock for both the data structs and registers
369 370
	 * Note: Not necessary to obtain the hardware lock, since the GPE
	 * registers are owned by the gpe_lock.
B
Bob Moore 已提交
371
	 */
L
Len Brown 已提交
372
	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
B
Bob Moore 已提交
373 374 375

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

L
Linus Torvalds 已提交
376 377
	gpe_block = gpe_xrupt_list->gpe_block_list_head;
	while (gpe_block) {
378 379
		gpe_device = gpe_block->node;

L
Linus Torvalds 已提交
380
		/*
381 382
		 * 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 已提交
383 384
		 */
		for (i = 0; i < gpe_block->register_count; i++) {
B
Bob Moore 已提交
385

L
Linus Torvalds 已提交
386 387 388 389
			/* Get the next status/enable pair */

			gpe_register_info = &gpe_block->register_info[i];

390 391 392 393 394 395
			/*
			 * 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)) {
396
				ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
397
						  "Ignore disabled registers for GPE %02X-%02X: "
398 399 400 401 402 403 404 405 406 407
						  "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));
408 409 410
				continue;
			}

L
Linus Torvalds 已提交
411 412 413
			/* Now look at the individual GPEs in this byte register */

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

415
				/* Detect and dispatch one GPE bit */
L
Linus Torvalds 已提交
416

417 418
				gpe_event_info =
				    &gpe_block->
419
				    event_info[((acpi_size)i *
420 421 422
						ACPI_GPE_REGISTER_WIDTH) + j];
				gpe_number =
				    j + gpe_register_info->base_gpe_number;
423 424 425 426 427 428
				acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
				int_status |=
				    acpi_ev_detect_gpe(gpe_device,
						       gpe_event_info,
						       gpe_number);
				flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
L
Linus Torvalds 已提交
429 430 431 432 433 434
			}
		}

		gpe_block = gpe_block->next;
	}

L
Len Brown 已提交
435
	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446
	return (int_status);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_asynch_execute_gpe_method
 *
 * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
 *
 * RETURN:      None
 *
B
Bob Moore 已提交
447 448 449
 * 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 已提交
450 451 452 453 454
 *              the control method itself is not executed in the context of
 *              an interrupt handler.
 *
 ******************************************************************************/

L
Len Brown 已提交
455
static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
L
Linus Torvalds 已提交
456
{
457
	struct acpi_gpe_event_info *gpe_event_info = context;
458
	acpi_status status = AE_OK;
B
Bob Moore 已提交
459
	struct acpi_evaluate_info *info;
460
	struct acpi_gpe_notify_info *notify;
L
Linus Torvalds 已提交
461

B
Bob Moore 已提交
462
	ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
L
Linus Torvalds 已提交
463

L
Lin Ming 已提交
464 465
	/* Do the correct dispatch - normal method or implicit notify */

466
	switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
L
Lin Ming 已提交
467 468 469 470 471 472 473 474
	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.
475 476 477
		 *
		 * June 2012: Expand implicit notify mechanism to support
		 * notifies on multiple device objects.
L
Lin Ming 已提交
478
		 */
479
		notify = gpe_event_info->dispatch.notify_list;
480 481 482 483 484 485
		while (ACPI_SUCCESS(status) && notify) {
			status =
			    acpi_ev_queue_notify_request(notify->device_node,
							 ACPI_NOTIFY_DEVICE_WAKE);

			notify = notify->next;
486 487
		}

L
Lin Ming 已提交
488 489 490
		break;

	case ACPI_GPE_DISPATCH_METHOD:
L
Linus Torvalds 已提交
491

B
Bob Moore 已提交
492 493 494 495 496 497 498
		/* Allocate the evaluation information block */

		info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
		if (!info) {
			status = AE_NO_MEMORY;
		} else {
			/*
L
Lv Zheng 已提交
499 500
			 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
			 * _Lxx/_Exx control method that corresponds to this GPE
B
Bob Moore 已提交
501 502
			 */
			info->prefix_node =
503
			    gpe_event_info->dispatch.method_node;
B
Bob Moore 已提交
504 505 506 507 508 509
			info->flags = ACPI_IGNORE_RETURN_VALUE;

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

L
Len Brown 已提交
510
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
511
			ACPI_EXCEPTION((AE_INFO, status,
B
Bob Moore 已提交
512
					"while evaluating GPE method [%4.4s]",
513 514 515
					acpi_ut_get_node_name(gpe_event_info->
							      dispatch.
							      method_node)));
L
Linus Torvalds 已提交
516
		}
L
Lin Ming 已提交
517 518 519
		break;

	default:
520

521
		goto error_exit;	/* Should never happen */
L
Linus Torvalds 已提交
522
	}
523

524
	/* Defer enabling of GPE until all notify handlers are done */
525 526

	status = acpi_os_execute(OSL_NOTIFY_HANDLER,
527 528 529
				 acpi_ev_asynch_enable_gpe, gpe_event_info);
	if (ACPI_SUCCESS(status)) {
		return_VOID;
530
	}
531 532 533

error_exit:
	acpi_ev_asynch_enable_gpe(gpe_event_info);
534 535
	return_VOID;
}
L
Linus Torvalds 已提交
536

L
Lin Ming 已提交
537

538 539 540 541 542 543 544 545 546 547
/*******************************************************************************
 *
 * 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 已提交
548
 *              complete (i.e., finish execution of Notify)
549 550 551 552
 *
 ******************************************************************************/

static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
553 554
{
	struct acpi_gpe_event_info *gpe_event_info = context;
555
	acpi_cpu_flags flags;
L
Lin Ming 已提交
556

557
	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
L
Lin Ming 已提交
558
	(void)acpi_ev_finish_gpe(gpe_event_info);
559
	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
L
Lin Ming 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

	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.
 *
 ******************************************************************************/

578
acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)
L
Lin Ming 已提交
579
{
580
	acpi_status status;
581

582
	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
L
Len Brown 已提交
583
	    ACPI_GPE_LEVEL_TRIGGERED) {
L
Linus Torvalds 已提交
584
		/*
L
Lin Ming 已提交
585 586
		 * GPE is level-triggered, we clear the GPE status bit after
		 * handling the event.
L
Linus Torvalds 已提交
587
		 */
588
		status = acpi_hw_clear_gpe(gpe_event_info);
L
Len Brown 已提交
589
		if (ACPI_FAILURE(status)) {
L
Lin Ming 已提交
590
			return (status);
L
Linus Torvalds 已提交
591 592 593
		}
	}

B
Bob Moore 已提交
594
	/*
L
Lin Ming 已提交
595
	 * Enable this GPE, conditionally. This means that the GPE will
596
	 * only be physically enabled if the enable_mask bit is set
L
Lin Ming 已提交
597
	 * in the event_info.
B
Bob Moore 已提交
598
	 */
599
	(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
600
	gpe_event_info->disable_for_dispatch = FALSE;
L
Lin Ming 已提交
601
	return (AE_OK);
L
Linus Torvalds 已提交
602 603
}

L
Lin Ming 已提交
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_detect_gpe
 *
 * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
 *              gpe_event_info      - Info for this GPE
 *              gpe_number          - Number relative to the parent GPE block
 *
 * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
 *
 * DESCRIPTION: Detect and dispatch a General Purpose Event to either a function
 *              (e.g. EC) or method (e.g. _Lxx/_Exx) handler.
 * NOTE:        GPE is W1C, so it is possible to handle a single GPE from both
 *              task and irq context in parallel as long as the process to
 *              detect and mask the GPE is atomic.
 *              However the atomicity of ACPI_GPE_DISPATCH_RAW_HANDLER is
 *              dependent on the raw handler itself.
 *
 ******************************************************************************/

u32
acpi_ev_detect_gpe(struct acpi_namespace_node *gpe_device,
		   struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
{
	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
	u8 enabled_status_byte;
	u64 status_reg;
	u64 enable_reg;
	u32 register_bit;
	struct acpi_gpe_register_info *gpe_register_info;
	struct acpi_gpe_handler_info *gpe_handler_info;
	acpi_cpu_flags flags;
	acpi_status status;

	ACPI_FUNCTION_TRACE(ev_gpe_detect);

	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);

643 644 645 646 647 648
	if (!gpe_event_info) {
		gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
		if (!gpe_event_info)
			goto error_exit;
	}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	/* Get the info block for the entire GPE register */

	gpe_register_info = gpe_event_info->register_info;

	/* Get the register bitmask for this GPE */

	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);

	/* GPE currently enabled (enable bit == 1)? */

	status = acpi_hw_read(&enable_reg, &gpe_register_info->enable_address);
	if (ACPI_FAILURE(status)) {
		goto error_exit;
	}

	/* GPE currently active (status bit == 1)? */

	status = acpi_hw_read(&status_reg, &gpe_register_info->status_address);
	if (ACPI_FAILURE(status)) {
		goto error_exit;
	}

	/* Check if there is anything active at all in this GPE */

	ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
			  "Read registers for GPE %02X: Status=%02X, Enable=%02X, "
			  "RunEnable=%02X, WakeEnable=%02X\n",
			  gpe_number,
			  (u32)(status_reg & register_bit),
			  (u32)(enable_reg & register_bit),
			  gpe_register_info->enable_for_run,
			  gpe_register_info->enable_for_wake));

	enabled_status_byte = (u8)(status_reg & enable_reg);
	if (!(enabled_status_byte & register_bit)) {
		goto error_exit;
	}

	/* 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);
	}

	/* Found an active GPE */

	if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
	    ACPI_GPE_DISPATCH_RAW_HANDLER) {

		/* Dispatch the event to a raw handler */

		gpe_handler_info = gpe_event_info->dispatch.handler;

		/*
		 * There is no protection around the namespace node
		 * and the GPE handler to ensure a safe destruction
		 * because:
		 * 1. The namespace node is expected to always
		 *    exist after loading a table.
		 * 2. The GPE handler is expected to be flushed by
		 *    acpi_os_wait_events_complete() before the
		 *    destruction.
		 */
		acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
		int_status |=
		    gpe_handler_info->address(gpe_device, gpe_number,
					      gpe_handler_info->context);
		flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
	} else {
		/* Dispatch the event to a standard handler or method. */

		int_status |= acpi_ev_gpe_dispatch(gpe_device,
						   gpe_event_info, gpe_number);
	}

error_exit:
	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
	return (int_status);
}

L
Linus Torvalds 已提交
732 733 734 735
/*******************************************************************************
 *
 * FUNCTION:    acpi_ev_gpe_dispatch
 *
736 737 738
 * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
 *              gpe_event_info      - Info for this GPE
 *              gpe_number          - Number relative to the parent GPE block
L
Linus Torvalds 已提交
739 740 741 742 743 744 745 746 747
 *
 * 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.
 *
 ******************************************************************************/

u32
748
acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
749
		     struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
L
Linus Torvalds 已提交
750
{
L
Len Brown 已提交
751
	acpi_status status;
L
Lin Ming 已提交
752
	u32 return_value;
L
Linus Torvalds 已提交
753

B
Bob Moore 已提交
754
	ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
L
Linus Torvalds 已提交
755 756

	/*
L
Lin Ming 已提交
757 758 759 760 761 762 763 764 765 766 767
	 * 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,
768
				"Unable to disable GPE %02X", gpe_number));
769
		return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
L
Lin Ming 已提交
770 771
	}

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
	/*
	 * If edge-triggered, clear the GPE status bit now. Note that
	 * level-triggered events are cleared after the GPE is serviced.
	 */
	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
	    ACPI_GPE_EDGE_TRIGGERED) {
		status = acpi_hw_clear_gpe(gpe_event_info);
		if (ACPI_FAILURE(status)) {
			ACPI_EXCEPTION((AE_INFO, status,
					"Unable to clear GPE %02X",
					gpe_number));
			(void)acpi_hw_low_set_gpe(gpe_event_info,
						  ACPI_GPE_CONDITIONAL_ENABLE);
			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
		}
	}

789 790
	gpe_event_info->disable_for_dispatch = TRUE;

L
Lin Ming 已提交
791 792 793 794 795 796
	/*
	 * 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 已提交
797
	 */
798
	switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
L
Linus Torvalds 已提交
799 800
	case ACPI_GPE_DISPATCH_HANDLER:

L
Lin Ming 已提交
801 802 803 804 805 806 807 808 809
		/* 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);

810
		/* If requested, clear (if level-triggered) and re-enable the GPE */
L
Lin Ming 已提交
811 812 813

		if (return_value & ACPI_REENABLE_GPE) {
			(void)acpi_ev_finish_gpe(gpe_event_info);
L
Linus Torvalds 已提交
814 815 816 817
		}
		break;

	case ACPI_GPE_DISPATCH_METHOD:
L
Lin Ming 已提交
818
	case ACPI_GPE_DISPATCH_NOTIFY:
L
Linus Torvalds 已提交
819 820 821 822
		/*
		 * Execute the method associated with the GPE
		 * NOTE: Level-triggered GPEs are cleared after the method completes.
		 */
B
Bob Moore 已提交
823 824 825
		status = acpi_os_execute(OSL_GPE_HANDLER,
					 acpi_ev_asynch_execute_gpe_method,
					 gpe_event_info);
L
Len Brown 已提交
826
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
827
			ACPI_EXCEPTION((AE_INFO, status,
828
					"Unable to queue handler for GPE %02X - event disabled",
B
Bob Moore 已提交
829
					gpe_number));
L
Linus Torvalds 已提交
830 831 832 833
		}
		break;

	default:
834 835 836 837 838
		/*
		 * 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 已提交
839
		ACPI_ERROR((AE_INFO,
840
			    "No handler or method for GPE %02X, disabling event",
B
Bob Moore 已提交
841
			    gpe_number));
L
Linus Torvalds 已提交
842 843 844 845

		break;
	}

846
	return_UINT32(ACPI_INTERRUPT_HANDLED);
L
Linus Torvalds 已提交
847
}
848 849

#endif				/* !ACPI_REDUCED_HARDWARE */