1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: evgpe - General Purpose Event handling and dispatch
6 * Copyright (C) 2000 - 2018, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
15 #define _COMPONENT ACPI_EVENTS
16 ACPI_MODULE_NAME("evgpe")
17 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
18 /* Local prototypes */
19 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
21 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);
23 /*******************************************************************************
25 * FUNCTION: acpi_ev_update_gpe_enable_mask
27 * PARAMETERS: gpe_event_info - GPE to update
31 * DESCRIPTION: Updates GPE register enable mask based upon whether there are
32 * runtime references to this GPE
34 ******************************************************************************/
37 acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
39 struct acpi_gpe_register_info *gpe_register_info;
42 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);
44 gpe_register_info = gpe_event_info->register_info;
45 if (!gpe_register_info) {
46 return_ACPI_STATUS(AE_NOT_EXIST);
49 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
51 /* Clear the run bit up front */
53 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
55 /* Set the mask bit only if there are references to this GPE */
57 if (gpe_event_info->runtime_count) {
58 ACPI_SET_BIT(gpe_register_info->enable_for_run,
62 gpe_register_info->enable_mask = gpe_register_info->enable_for_run;
63 return_ACPI_STATUS(AE_OK);
66 /*******************************************************************************
68 * FUNCTION: acpi_ev_enable_gpe
70 * PARAMETERS: gpe_event_info - GPE to enable
74 * DESCRIPTION: Enable a GPE.
76 ******************************************************************************/
78 acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
82 ACPI_FUNCTION_TRACE(ev_enable_gpe);
84 /* Enable the requested GPE */
86 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
87 return_ACPI_STATUS(status);
90 /*******************************************************************************
92 * FUNCTION: acpi_ev_mask_gpe
94 * PARAMETERS: gpe_event_info - GPE to be blocked/unblocked
95 * is_masked - Whether the GPE is masked or not
99 * DESCRIPTION: Unconditionally mask/unmask a GPE during runtime.
101 ******************************************************************************/
104 acpi_ev_mask_gpe(struct acpi_gpe_event_info *gpe_event_info, u8 is_masked)
106 struct acpi_gpe_register_info *gpe_register_info;
109 ACPI_FUNCTION_TRACE(ev_mask_gpe);
111 gpe_register_info = gpe_event_info->register_info;
112 if (!gpe_register_info) {
113 return_ACPI_STATUS(AE_NOT_EXIST);
116 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
118 /* Perform the action */
121 if (register_bit & gpe_register_info->mask_for_run) {
122 return_ACPI_STATUS(AE_BAD_PARAMETER);
125 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
126 ACPI_SET_BIT(gpe_register_info->mask_for_run, (u8)register_bit);
128 if (!(register_bit & gpe_register_info->mask_for_run)) {
129 return_ACPI_STATUS(AE_BAD_PARAMETER);
132 ACPI_CLEAR_BIT(gpe_register_info->mask_for_run,
134 if (gpe_event_info->runtime_count
135 && !gpe_event_info->disable_for_dispatch) {
136 (void)acpi_hw_low_set_gpe(gpe_event_info,
141 return_ACPI_STATUS(AE_OK);
144 /*******************************************************************************
146 * FUNCTION: acpi_ev_add_gpe_reference
148 * PARAMETERS: gpe_event_info - Add a reference to this GPE
152 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
155 ******************************************************************************/
158 acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
160 acpi_status status = AE_OK;
162 ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
164 if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
165 return_ACPI_STATUS(AE_LIMIT);
168 gpe_event_info->runtime_count++;
169 if (gpe_event_info->runtime_count == 1) {
171 /* Enable on first reference */
173 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
174 if (ACPI_SUCCESS(status)) {
175 status = acpi_ev_enable_gpe(gpe_event_info);
178 if (ACPI_FAILURE(status)) {
179 gpe_event_info->runtime_count--;
183 return_ACPI_STATUS(status);
186 /*******************************************************************************
188 * FUNCTION: acpi_ev_remove_gpe_reference
190 * PARAMETERS: gpe_event_info - Remove a reference to this GPE
194 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
195 * removed, the GPE is hardware-disabled.
197 ******************************************************************************/
200 acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
202 acpi_status status = AE_OK;
204 ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
206 if (!gpe_event_info->runtime_count) {
207 return_ACPI_STATUS(AE_LIMIT);
210 gpe_event_info->runtime_count--;
211 if (!gpe_event_info->runtime_count) {
213 /* Disable on last reference */
215 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
216 if (ACPI_SUCCESS(status)) {
218 acpi_hw_low_set_gpe(gpe_event_info,
222 if (ACPI_FAILURE(status)) {
223 gpe_event_info->runtime_count++;
227 return_ACPI_STATUS(status);
230 /*******************************************************************************
232 * FUNCTION: acpi_ev_low_get_gpe_info
234 * PARAMETERS: gpe_number - Raw GPE number
235 * gpe_block - A GPE info block
237 * RETURN: A GPE event_info struct. NULL if not a valid GPE (The gpe_number
238 * is not within the specified GPE block)
240 * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
241 * the low-level implementation of ev_get_gpe_event_info.
243 ******************************************************************************/
245 struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
246 struct acpi_gpe_block_info
252 * Validate that the gpe_number is within the specified gpe_block.
255 if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
259 gpe_index = gpe_number - gpe_block->block_base_number;
260 if (gpe_index >= gpe_block->gpe_count) {
264 return (&gpe_block->event_info[gpe_index]);
268 /*******************************************************************************
270 * FUNCTION: acpi_ev_get_gpe_event_info
272 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
273 * gpe_number - Raw GPE number
275 * RETURN: A GPE event_info struct. NULL if not a valid GPE
277 * DESCRIPTION: Returns the event_info struct associated with this GPE.
278 * Validates the gpe_block and the gpe_number
280 * Should be called only when the GPE lists are semaphore locked
281 * and not subject to change.
283 ******************************************************************************/
285 struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
288 union acpi_operand_object *obj_desc;
289 struct acpi_gpe_event_info *gpe_info;
292 ACPI_FUNCTION_ENTRY();
294 /* A NULL gpe_device means use the FADT-defined GPE block(s) */
298 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
300 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
301 gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
302 acpi_gbl_gpe_fadt_blocks
309 /* The gpe_number was not in the range of either FADT GPE block */
314 /* A Non-NULL gpe_device means this is a GPE Block Device */
317 acpi_ns_get_attached_object((struct acpi_namespace_node *)
319 if (!obj_desc || !obj_desc->device.gpe_block) {
323 return (acpi_ev_low_get_gpe_info
324 (gpe_number, obj_desc->device.gpe_block));
327 /*******************************************************************************
329 * FUNCTION: acpi_ev_gpe_detect
331 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
332 * Can have multiple GPE blocks attached.
334 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
336 * DESCRIPTION: Detect if any GP events have occurred. This function is
337 * executed at interrupt level.
339 ******************************************************************************/
341 u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list)
343 struct acpi_gpe_block_info *gpe_block;
344 struct acpi_namespace_node *gpe_device;
345 struct acpi_gpe_register_info *gpe_register_info;
346 struct acpi_gpe_event_info *gpe_event_info;
348 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
349 acpi_cpu_flags flags;
353 ACPI_FUNCTION_NAME(ev_gpe_detect);
355 /* Check for the case where there are no GPEs */
357 if (!gpe_xrupt_list) {
362 * We need to obtain the GPE lock for both the data structs and registers
363 * Note: Not necessary to obtain the hardware lock, since the GPE
364 * registers are owned by the gpe_lock.
366 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
368 /* Examine all GPE blocks attached to this interrupt level */
370 gpe_block = gpe_xrupt_list->gpe_block_list_head;
372 gpe_device = gpe_block->node;
375 * Read all of the 8-bit GPE status and enable registers in this GPE
376 * block, saving all of them. Find all currently active GP events.
378 for (i = 0; i < gpe_block->register_count; i++) {
380 /* Get the next status/enable pair */
382 gpe_register_info = &gpe_block->register_info[i];
385 * Optimization: If there are no GPEs enabled within this
386 * register, we can safely ignore the entire register.
388 if (!(gpe_register_info->enable_for_run |
389 gpe_register_info->enable_for_wake)) {
390 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
391 "Ignore disabled registers for GPE %02X-%02X: "
392 "RunEnable=%02X, WakeEnable=%02X\n",
397 (ACPI_GPE_REGISTER_WIDTH - 1),
405 /* Now look at the individual GPEs in this byte register */
407 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
409 /* Detect and dispatch one GPE bit */
413 event_info[((acpi_size)i *
414 ACPI_GPE_REGISTER_WIDTH) + j];
416 j + gpe_register_info->base_gpe_number;
417 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
419 acpi_ev_detect_gpe(gpe_device,
422 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
426 gpe_block = gpe_block->next;
429 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
433 /*******************************************************************************
435 * FUNCTION: acpi_ev_asynch_execute_gpe_method
437 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
441 * DESCRIPTION: Perform the actual execution of a GPE control method. This
442 * function is called from an invocation of acpi_os_execute and
443 * therefore does NOT execute at interrupt level - so that
444 * the control method itself is not executed in the context of
445 * an interrupt handler.
447 ******************************************************************************/
449 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
451 struct acpi_gpe_event_info *gpe_event_info = context;
452 acpi_status status = AE_OK;
453 struct acpi_evaluate_info *info;
454 struct acpi_gpe_notify_info *notify;
456 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
458 /* Do the correct dispatch - normal method or implicit notify */
460 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
461 case ACPI_GPE_DISPATCH_NOTIFY:
464 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
465 * NOTE: the request is queued for execution after this method
466 * completes. The notify handlers are NOT invoked synchronously
467 * from this thread -- because handlers may in turn run other
470 * June 2012: Expand implicit notify mechanism to support
471 * notifies on multiple device objects.
473 notify = gpe_event_info->dispatch.notify_list;
474 while (ACPI_SUCCESS(status) && notify) {
476 acpi_ev_queue_notify_request(notify->device_node,
477 ACPI_NOTIFY_DEVICE_WAKE);
479 notify = notify->next;
484 case ACPI_GPE_DISPATCH_METHOD:
486 /* Allocate the evaluation information block */
488 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
490 status = AE_NO_MEMORY;
493 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
494 * _Lxx/_Exx control method that corresponds to this GPE
497 gpe_event_info->dispatch.method_node;
498 info->flags = ACPI_IGNORE_RETURN_VALUE;
500 status = acpi_ns_evaluate(info);
504 if (ACPI_FAILURE(status)) {
505 ACPI_EXCEPTION((AE_INFO, status,
506 "while evaluating GPE method [%4.4s]",
507 acpi_ut_get_node_name(gpe_event_info->
515 goto error_exit; /* Should never happen */
518 /* Defer enabling of GPE until all notify handlers are done */
520 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
521 acpi_ev_asynch_enable_gpe, gpe_event_info);
522 if (ACPI_SUCCESS(status)) {
527 acpi_ev_asynch_enable_gpe(gpe_event_info);
532 /*******************************************************************************
534 * FUNCTION: acpi_ev_asynch_enable_gpe
536 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
537 * Callback from acpi_os_execute
541 * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
542 * complete (i.e., finish execution of Notify)
544 ******************************************************************************/
546 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
548 struct acpi_gpe_event_info *gpe_event_info = context;
549 acpi_cpu_flags flags;
551 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
552 (void)acpi_ev_finish_gpe(gpe_event_info);
553 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
559 /*******************************************************************************
561 * FUNCTION: acpi_ev_finish_gpe
563 * PARAMETERS: gpe_event_info - Info for this GPE
567 * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
568 * of a GPE method or a synchronous or asynchronous GPE handler.
570 ******************************************************************************/
572 acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info *gpe_event_info)
576 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
577 ACPI_GPE_LEVEL_TRIGGERED) {
579 * GPE is level-triggered, we clear the GPE status bit after
580 * handling the event.
582 status = acpi_hw_clear_gpe(gpe_event_info);
583 if (ACPI_FAILURE(status)) {
589 * Enable this GPE, conditionally. This means that the GPE will
590 * only be physically enabled if the enable_mask bit is set
593 (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
594 gpe_event_info->disable_for_dispatch = FALSE;
599 /*******************************************************************************
601 * FUNCTION: acpi_ev_detect_gpe
603 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
604 * gpe_event_info - Info for this GPE
605 * gpe_number - Number relative to the parent GPE block
607 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
609 * DESCRIPTION: Detect and dispatch a General Purpose Event to either a function
610 * (e.g. EC) or method (e.g. _Lxx/_Exx) handler.
611 * NOTE: GPE is W1C, so it is possible to handle a single GPE from both
612 * task and irq context in parallel as long as the process to
613 * detect and mask the GPE is atomic.
614 * However the atomicity of ACPI_GPE_DISPATCH_RAW_HANDLER is
615 * dependent on the raw handler itself.
617 ******************************************************************************/
620 acpi_ev_detect_gpe(struct acpi_namespace_node *gpe_device,
621 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
623 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
624 u8 enabled_status_byte;
628 struct acpi_gpe_register_info *gpe_register_info;
629 struct acpi_gpe_handler_info *gpe_handler_info;
630 acpi_cpu_flags flags;
633 ACPI_FUNCTION_TRACE(ev_gpe_detect);
635 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
637 /* Get the info block for the entire GPE register */
639 gpe_register_info = gpe_event_info->register_info;
641 /* Get the register bitmask for this GPE */
643 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
645 /* GPE currently enabled (enable bit == 1)? */
647 status = acpi_hw_read(&enable_reg, &gpe_register_info->enable_address);
648 if (ACPI_FAILURE(status)) {
652 /* GPE currently active (status bit == 1)? */
654 status = acpi_hw_read(&status_reg, &gpe_register_info->status_address);
655 if (ACPI_FAILURE(status)) {
659 /* Check if there is anything active at all in this GPE */
661 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
662 "Read registers for GPE %02X: Status=%02X, Enable=%02X, "
663 "RunEnable=%02X, WakeEnable=%02X\n",
665 (u32)(status_reg & register_bit),
666 (u32)(enable_reg & register_bit),
667 gpe_register_info->enable_for_run,
668 gpe_register_info->enable_for_wake));
670 enabled_status_byte = (u8)(status_reg & enable_reg);
671 if (!(enabled_status_byte & register_bit)) {
675 /* Invoke global event handler if present */
678 if (acpi_gbl_global_event_handler) {
679 acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE,
680 gpe_device, gpe_number,
681 acpi_gbl_global_event_handler_context);
684 /* Found an active GPE */
686 if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
687 ACPI_GPE_DISPATCH_RAW_HANDLER) {
689 /* Dispatch the event to a raw handler */
691 gpe_handler_info = gpe_event_info->dispatch.handler;
694 * There is no protection around the namespace node
695 * and the GPE handler to ensure a safe destruction
697 * 1. The namespace node is expected to always
698 * exist after loading a table.
699 * 2. The GPE handler is expected to be flushed by
700 * acpi_os_wait_events_complete() before the
703 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
705 gpe_handler_info->address(gpe_device, gpe_number,
706 gpe_handler_info->context);
707 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
709 /* Dispatch the event to a standard handler or method. */
711 int_status |= acpi_ev_gpe_dispatch(gpe_device,
712 gpe_event_info, gpe_number);
716 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
720 /*******************************************************************************
722 * FUNCTION: acpi_ev_gpe_dispatch
724 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
725 * gpe_event_info - Info for this GPE
726 * gpe_number - Number relative to the parent GPE block
728 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
730 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
731 * or method (e.g. _Lxx/_Exx) handler.
733 ******************************************************************************/
736 acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
737 struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
742 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
745 * Always disable the GPE so that it does not keep firing before
746 * any asynchronous activity completes (either from the execution
747 * of a GPE method or an asynchronous GPE handler.)
749 * If there is no handler or method to run, just disable the
750 * GPE and leave it disabled permanently to prevent further such
751 * pointless events from firing.
753 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
754 if (ACPI_FAILURE(status)) {
755 ACPI_EXCEPTION((AE_INFO, status,
756 "Unable to disable GPE %02X", gpe_number));
757 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
761 * If edge-triggered, clear the GPE status bit now. Note that
762 * level-triggered events are cleared after the GPE is serviced.
764 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
765 ACPI_GPE_EDGE_TRIGGERED) {
766 status = acpi_hw_clear_gpe(gpe_event_info);
767 if (ACPI_FAILURE(status)) {
768 ACPI_EXCEPTION((AE_INFO, status,
769 "Unable to clear GPE %02X",
771 (void)acpi_hw_low_set_gpe(gpe_event_info,
772 ACPI_GPE_CONDITIONAL_ENABLE);
773 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
777 gpe_event_info->disable_for_dispatch = TRUE;
780 * Dispatch the GPE to either an installed handler or the control
781 * method associated with this GPE (_Lxx or _Exx). If a handler
782 * exists, we invoke it and do not attempt to run the method.
783 * If there is neither a handler nor a method, leave the GPE
786 switch (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags)) {
787 case ACPI_GPE_DISPATCH_HANDLER:
789 /* Invoke the installed handler (at interrupt level) */
792 gpe_event_info->dispatch.handler->address(gpe_device,
798 /* If requested, clear (if level-triggered) and reenable the GPE */
800 if (return_value & ACPI_REENABLE_GPE) {
801 (void)acpi_ev_finish_gpe(gpe_event_info);
805 case ACPI_GPE_DISPATCH_METHOD:
806 case ACPI_GPE_DISPATCH_NOTIFY:
808 * Execute the method associated with the GPE
809 * NOTE: Level-triggered GPEs are cleared after the method completes.
811 status = acpi_os_execute(OSL_GPE_HANDLER,
812 acpi_ev_asynch_execute_gpe_method,
814 if (ACPI_FAILURE(status)) {
815 ACPI_EXCEPTION((AE_INFO, status,
816 "Unable to queue handler for GPE %02X - event disabled",
823 * No handler or method to run!
824 * 03/2010: This case should no longer be possible. We will not allow
825 * a GPE to be enabled if it has no handler or method.
828 "No handler or method for GPE %02X, disabling event",
834 return_UINT32(ACPI_INTERRUPT_HANDLED);
837 #endif /* !ACPI_REDUCED_HARDWARE */