Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / acpi / events / evgpe.c
blob0dadd2adc8001a109669b4ae62c673da60f4747b
1 /******************************************************************************
3 * Module Name: evgpe - General Purpose Event handling and dispatch
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2007, R. Byron Moore
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
45 #include <acpi/acevents.h>
46 #include <acpi/acnamesp.h>
48 #define _COMPONENT ACPI_EVENTS
49 ACPI_MODULE_NAME("evgpe")
51 /* Local prototypes */
52 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
54 /*******************************************************************************
56 * FUNCTION: acpi_ev_set_gpe_type
58 * PARAMETERS: gpe_event_info - GPE to set
59 * Type - New type
61 * RETURN: Status
63 * DESCRIPTION: Sets the new type for the GPE (wake, run, or wake/run)
65 ******************************************************************************/
67 acpi_status
68 acpi_ev_set_gpe_type(struct acpi_gpe_event_info *gpe_event_info, u8 type)
70 acpi_status status;
72 ACPI_FUNCTION_TRACE(ev_set_gpe_type);
74 /* Validate type and update register enable masks */
76 switch (type) {
77 case ACPI_GPE_TYPE_WAKE:
78 case ACPI_GPE_TYPE_RUNTIME:
79 case ACPI_GPE_TYPE_WAKE_RUN:
80 break;
82 default:
83 return_ACPI_STATUS(AE_BAD_PARAMETER);
86 /* Disable the GPE if currently enabled */
88 status = acpi_ev_disable_gpe(gpe_event_info);
90 /* Type was validated above */
92 gpe_event_info->flags &= ~ACPI_GPE_TYPE_MASK; /* Clear type bits */
93 gpe_event_info->flags |= type; /* Insert type */
94 return_ACPI_STATUS(status);
97 /*******************************************************************************
99 * FUNCTION: acpi_ev_update_gpe_enable_masks
101 * PARAMETERS: gpe_event_info - GPE to update
102 * Type - What to do: ACPI_GPE_DISABLE or
103 * ACPI_GPE_ENABLE
105 * RETURN: Status
107 * DESCRIPTION: Updates GPE register enable masks based on the GPE type
109 ******************************************************************************/
111 acpi_status
112 acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info,
113 u8 type)
115 struct acpi_gpe_register_info *gpe_register_info;
116 u8 register_bit;
118 ACPI_FUNCTION_TRACE(ev_update_gpe_enable_masks);
120 gpe_register_info = gpe_event_info->register_info;
121 if (!gpe_register_info) {
122 return_ACPI_STATUS(AE_NOT_EXIST);
124 register_bit = (u8)
125 (1 <<
126 (gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));
128 /* 1) Disable case. Simply clear all enable bits */
130 if (type == ACPI_GPE_DISABLE) {
131 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
132 register_bit);
133 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
134 return_ACPI_STATUS(AE_OK);
137 /* 2) Enable case. Set/Clear the appropriate enable bits */
139 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
140 case ACPI_GPE_TYPE_WAKE:
141 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
142 ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
143 break;
145 case ACPI_GPE_TYPE_RUNTIME:
146 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
147 register_bit);
148 ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
149 break;
151 case ACPI_GPE_TYPE_WAKE_RUN:
152 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
153 ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
154 break;
156 default:
157 return_ACPI_STATUS(AE_BAD_PARAMETER);
160 return_ACPI_STATUS(AE_OK);
163 /*******************************************************************************
165 * FUNCTION: acpi_ev_enable_gpe
167 * PARAMETERS: gpe_event_info - GPE to enable
168 * write_to_hardware - Enable now, or just mark data structs
169 * (WAKE GPEs should be deferred)
171 * RETURN: Status
173 * DESCRIPTION: Enable a GPE based on the GPE type
175 ******************************************************************************/
177 acpi_status
178 acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
179 u8 write_to_hardware)
181 acpi_status status;
183 ACPI_FUNCTION_TRACE(ev_enable_gpe);
185 /* Make sure HW enable masks are updated */
187 status =
188 acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_ENABLE);
189 if (ACPI_FAILURE(status)) {
190 return_ACPI_STATUS(status);
193 /* Mark wake-enabled or HW enable, or both */
195 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
196 case ACPI_GPE_TYPE_WAKE:
198 ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
199 break;
201 case ACPI_GPE_TYPE_WAKE_RUN:
203 ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
205 /*lint -fallthrough */
207 case ACPI_GPE_TYPE_RUNTIME:
209 ACPI_SET_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
211 if (write_to_hardware) {
213 /* Clear the GPE (of stale events), then enable it */
215 status = acpi_hw_clear_gpe(gpe_event_info);
216 if (ACPI_FAILURE(status)) {
217 return_ACPI_STATUS(status);
220 /* Enable the requested runtime GPE */
222 status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
224 break;
226 default:
227 return_ACPI_STATUS(AE_BAD_PARAMETER);
230 return_ACPI_STATUS(AE_OK);
233 /*******************************************************************************
235 * FUNCTION: acpi_ev_disable_gpe
237 * PARAMETERS: gpe_event_info - GPE to disable
239 * RETURN: Status
241 * DESCRIPTION: Disable a GPE based on the GPE type
243 ******************************************************************************/
245 acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
247 acpi_status status;
249 ACPI_FUNCTION_TRACE(ev_disable_gpe);
251 if (!(gpe_event_info->flags & ACPI_GPE_ENABLE_MASK)) {
252 return_ACPI_STATUS(AE_OK);
255 /* Make sure HW enable masks are updated */
257 status =
258 acpi_ev_update_gpe_enable_masks(gpe_event_info, ACPI_GPE_DISABLE);
259 if (ACPI_FAILURE(status)) {
260 return_ACPI_STATUS(status);
263 /* Mark wake-disabled or HW disable, or both */
265 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
266 case ACPI_GPE_TYPE_WAKE:
267 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
268 break;
270 case ACPI_GPE_TYPE_WAKE_RUN:
271 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_WAKE_ENABLED);
273 /* fallthrough */
275 case ACPI_GPE_TYPE_RUNTIME:
277 /* Disable the requested runtime GPE */
279 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
281 /* fallthrough */
283 default:
284 acpi_hw_write_gpe_enable_reg(gpe_event_info);
287 return_ACPI_STATUS(AE_OK);
290 /*******************************************************************************
292 * FUNCTION: acpi_ev_get_gpe_event_info
294 * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
295 * gpe_number - Raw GPE number
297 * RETURN: A GPE event_info struct. NULL if not a valid GPE
299 * DESCRIPTION: Returns the event_info struct associated with this GPE.
300 * Validates the gpe_block and the gpe_number
302 * Should be called only when the GPE lists are semaphore locked
303 * and not subject to change.
305 ******************************************************************************/
307 struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
308 u32 gpe_number)
310 union acpi_operand_object *obj_desc;
311 struct acpi_gpe_block_info *gpe_block;
312 acpi_native_uint i;
314 ACPI_FUNCTION_ENTRY();
316 /* A NULL gpe_block means use the FADT-defined GPE block(s) */
318 if (!gpe_device) {
320 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
322 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
323 gpe_block = acpi_gbl_gpe_fadt_blocks[i];
324 if (gpe_block) {
325 if ((gpe_number >= gpe_block->block_base_number)
326 && (gpe_number <
327 gpe_block->block_base_number +
328 (gpe_block->register_count * 8))) {
329 return (&gpe_block->
330 event_info[gpe_number -
331 gpe_block->
332 block_base_number]);
337 /* The gpe_number was not in the range of either FADT GPE block */
339 return (NULL);
342 /* A Non-NULL gpe_device means this is a GPE Block Device */
344 obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
345 gpe_device);
346 if (!obj_desc || !obj_desc->device.gpe_block) {
347 return (NULL);
350 gpe_block = obj_desc->device.gpe_block;
352 if ((gpe_number >= gpe_block->block_base_number) &&
353 (gpe_number <
354 gpe_block->block_base_number + (gpe_block->register_count * 8))) {
355 return (&gpe_block->
356 event_info[gpe_number - gpe_block->block_base_number]);
359 return (NULL);
362 /*******************************************************************************
364 * FUNCTION: acpi_ev_gpe_detect
366 * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
367 * Can have multiple GPE blocks attached.
369 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
371 * DESCRIPTION: Detect if any GP events have occurred. This function is
372 * executed at interrupt level.
374 ******************************************************************************/
376 u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
378 acpi_status status;
379 struct acpi_gpe_block_info *gpe_block;
380 struct acpi_gpe_register_info *gpe_register_info;
381 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
382 u8 enabled_status_byte;
383 u32 status_reg;
384 u32 enable_reg;
385 acpi_cpu_flags flags;
386 acpi_native_uint i;
387 acpi_native_uint j;
389 ACPI_FUNCTION_NAME(ev_gpe_detect);
391 /* Check for the case where there are no GPEs */
393 if (!gpe_xrupt_list) {
394 return (int_status);
398 * We need to obtain the GPE lock for both the data structs and registers
399 * Note: Not necessary to obtain the hardware lock, since the GPE registers
400 * are owned by the gpe_lock.
402 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
404 /* Examine all GPE blocks attached to this interrupt level */
406 gpe_block = gpe_xrupt_list->gpe_block_list_head;
407 while (gpe_block) {
409 * Read all of the 8-bit GPE status and enable registers
410 * in this GPE block, saving all of them.
411 * Find all currently active GP events.
413 for (i = 0; i < gpe_block->register_count; i++) {
415 /* Get the next status/enable pair */
417 gpe_register_info = &gpe_block->register_info[i];
419 /* Read the Status Register */
421 status =
422 acpi_hw_low_level_read(ACPI_GPE_REGISTER_WIDTH,
423 &status_reg,
424 &gpe_register_info->
425 status_address);
426 if (ACPI_FAILURE(status)) {
427 goto unlock_and_exit;
430 /* Read the Enable Register */
432 status =
433 acpi_hw_low_level_read(ACPI_GPE_REGISTER_WIDTH,
434 &enable_reg,
435 &gpe_register_info->
436 enable_address);
437 if (ACPI_FAILURE(status)) {
438 goto unlock_and_exit;
441 ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
442 "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
443 gpe_register_info->base_gpe_number,
444 status_reg, enable_reg));
446 /* Check if there is anything active at all in this register */
448 enabled_status_byte = (u8) (status_reg & enable_reg);
449 if (!enabled_status_byte) {
451 /* No active GPEs in this register, move on */
453 continue;
456 /* Now look at the individual GPEs in this byte register */
458 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
460 /* Examine one GPE bit */
462 if (enabled_status_byte & (1 << j)) {
464 * Found an active GPE. Dispatch the event to a handler
465 * or method.
467 int_status |=
468 acpi_ev_gpe_dispatch(&gpe_block->
469 event_info[(i *
470 ACPI_GPE_REGISTER_WIDTH)
473 (u32) j +
474 gpe_register_info->
475 base_gpe_number);
480 gpe_block = gpe_block->next;
483 unlock_and_exit:
485 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
486 return (int_status);
489 /*******************************************************************************
491 * FUNCTION: acpi_ev_asynch_execute_gpe_method
493 * PARAMETERS: Context (gpe_event_info) - Info for this GPE
495 * RETURN: None
497 * DESCRIPTION: Perform the actual execution of a GPE control method. This
498 * function is called from an invocation of acpi_os_execute and
499 * therefore does NOT execute at interrupt level - so that
500 * the control method itself is not executed in the context of
501 * an interrupt handler.
503 ******************************************************************************/
504 static void acpi_ev_asynch_enable_gpe(void *context);
506 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
508 struct acpi_gpe_event_info *gpe_event_info = (void *)context;
509 acpi_status status;
510 struct acpi_gpe_event_info local_gpe_event_info;
511 struct acpi_evaluate_info *info;
513 ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
515 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
516 if (ACPI_FAILURE(status)) {
517 return_VOID;
520 /* Must revalidate the gpe_number/gpe_block */
522 if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
523 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
524 return_VOID;
527 /* Set the GPE flags for return to enabled state */
529 (void)acpi_ev_enable_gpe(gpe_event_info, FALSE);
532 * Take a snapshot of the GPE info for this level - we copy the
533 * info to prevent a race condition with remove_handler/remove_block.
535 ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
536 sizeof(struct acpi_gpe_event_info));
538 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
539 if (ACPI_FAILURE(status)) {
540 return_VOID;
544 * Must check for control method type dispatch one more
545 * time to avoid race with ev_gpe_install_handler
547 if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
548 ACPI_GPE_DISPATCH_METHOD) {
550 /* Allocate the evaluation information block */
552 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
553 if (!info) {
554 status = AE_NO_MEMORY;
555 } else {
557 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
558 * control method that corresponds to this GPE
560 info->prefix_node =
561 local_gpe_event_info.dispatch.method_node;
562 info->parameters =
563 ACPI_CAST_PTR(union acpi_operand_object *,
564 gpe_event_info);
565 info->parameter_type = ACPI_PARAM_GPE;
566 info->flags = ACPI_IGNORE_RETURN_VALUE;
568 status = acpi_ns_evaluate(info);
569 ACPI_FREE(info);
572 if (ACPI_FAILURE(status)) {
573 ACPI_EXCEPTION((AE_INFO, status,
574 "while evaluating GPE method [%4.4s]",
575 acpi_ut_get_node_name
576 (local_gpe_event_info.dispatch.
577 method_node)));
580 /* Defer enabling of GPE until all notify handlers are done */
581 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
582 gpe_event_info);
583 return_VOID;
586 static void acpi_ev_asynch_enable_gpe(void *context)
588 struct acpi_gpe_event_info *gpe_event_info = context;
589 acpi_status status;
590 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
591 ACPI_GPE_LEVEL_TRIGGERED) {
593 * GPE is level-triggered, we clear the GPE status bit after
594 * handling the event.
596 status = acpi_hw_clear_gpe(gpe_event_info);
597 if (ACPI_FAILURE(status)) {
598 return_VOID;
602 /* Enable this GPE */
603 (void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
604 return_VOID;
607 /*******************************************************************************
609 * FUNCTION: acpi_ev_gpe_dispatch
611 * PARAMETERS: gpe_event_info - Info for this GPE
612 * gpe_number - Number relative to the parent GPE block
614 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
616 * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
617 * or method (e.g. _Lxx/_Exx) handler.
619 * This function executes at interrupt level.
621 ******************************************************************************/
624 acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
626 acpi_status status;
628 ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
630 acpi_os_gpe_count(gpe_number);
633 * If edge-triggered, clear the GPE status bit now. Note that
634 * level-triggered events are cleared after the GPE is serviced.
636 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
637 ACPI_GPE_EDGE_TRIGGERED) {
638 status = acpi_hw_clear_gpe(gpe_event_info);
639 if (ACPI_FAILURE(status)) {
640 ACPI_EXCEPTION((AE_INFO, status,
641 "Unable to clear GPE[%2X]",
642 gpe_number));
643 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
648 * Dispatch the GPE to either an installed handler, or the control method
649 * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
650 * it and do not attempt to run the method. If there is neither a handler
651 * nor a method, we disable this GPE to prevent further such pointless
652 * events from firing.
654 switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
655 case ACPI_GPE_DISPATCH_HANDLER:
658 * Invoke the installed handler (at interrupt level)
659 * Ignore return status for now. TBD: leave GPE disabled on error?
661 (void)gpe_event_info->dispatch.handler->address(gpe_event_info->
662 dispatch.
663 handler->
664 context);
666 /* It is now safe to clear level-triggered events. */
668 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
669 ACPI_GPE_LEVEL_TRIGGERED) {
670 status = acpi_hw_clear_gpe(gpe_event_info);
671 if (ACPI_FAILURE(status)) {
672 ACPI_EXCEPTION((AE_INFO, status,
673 "Unable to clear GPE[%2X]",
674 gpe_number));
675 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
678 break;
680 case ACPI_GPE_DISPATCH_METHOD:
683 * Disable the GPE, so it doesn't keep firing before the method has a
684 * chance to run (it runs asynchronously with interrupts enabled).
686 status = acpi_ev_disable_gpe(gpe_event_info);
687 if (ACPI_FAILURE(status)) {
688 ACPI_EXCEPTION((AE_INFO, status,
689 "Unable to disable GPE[%2X]",
690 gpe_number));
691 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
695 * Execute the method associated with the GPE
696 * NOTE: Level-triggered GPEs are cleared after the method completes.
698 status = acpi_os_execute(OSL_GPE_HANDLER,
699 acpi_ev_asynch_execute_gpe_method,
700 gpe_event_info);
701 if (ACPI_FAILURE(status)) {
702 ACPI_EXCEPTION((AE_INFO, status,
703 "Unable to queue handler for GPE[%2X] - event disabled",
704 gpe_number));
706 break;
708 default:
710 /* No handler or method to run! */
712 ACPI_ERROR((AE_INFO,
713 "No handler or method for GPE[%2X], disabling event",
714 gpe_number));
717 * Disable the GPE. The GPE will remain disabled until the ACPI
718 * Core Subsystem is restarted, or a handler is installed.
720 status = acpi_ev_disable_gpe(gpe_event_info);
721 if (ACPI_FAILURE(status)) {
722 ACPI_EXCEPTION((AE_INFO, status,
723 "Unable to disable GPE[%2X]",
724 gpe_number));
725 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
727 break;
730 return_UINT32(ACPI_INTERRUPT_HANDLED);