1 /******************************************************************************
3 * Module Name: evevent - Fixed and General Purpose Acpi_event
4 * handling and dispatch
7 *****************************************************************************/
10 * Copyright (C) 2000, 2001 R. Byron Moore
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define _COMPONENT ACPI_EVENTS
30 MODULE_NAME ("evevent")
33 /**************************************************************************
35 * FUNCTION: Acpi_ev_initialize
41 * DESCRIPTION: Ensures that the system control interrupt (SCI) is properly
42 * configured, disables SCI event sources, installs the SCI
45 *************************************************************************/
54 /* Make sure we have ACPI tables */
57 return (AE_NO_ACPI_TABLES
);
61 /* Make sure the BIOS supports ACPI mode */
63 if (SYS_MODE_LEGACY
== acpi_hw_get_mode_capabilities()) {
68 acpi_gbl_original_mode
= acpi_hw_get_mode();
71 * Initialize the Fixed and General Purpose Acpi_events prior. This is
72 * done prior to enabling SCIs to prevent interrupts from occuring
73 * before handers are installed.
76 status
= acpi_ev_fixed_event_initialize ();
77 if (ACPI_FAILURE (status
)) {
81 status
= acpi_ev_gpe_initialize ();
82 if (ACPI_FAILURE (status
)) {
86 /* Install the SCI handler */
88 status
= acpi_ev_install_sci_handler ();
89 if (ACPI_FAILURE (status
)) {
94 /* Install handlers for control method GPE handlers (_Lxx, _Exx) */
96 status
= acpi_ev_init_gpe_control_methods ();
97 if (ACPI_FAILURE (status
)) {
101 /* Install the handler for the Global Lock */
103 status
= acpi_ev_init_global_lock_handler ();
104 if (ACPI_FAILURE (status
)) {
113 /******************************************************************************
115 * FUNCTION: Acpi_ev_fixed_event_initialize
121 * DESCRIPTION: Initialize the Fixed Acpi_event data structures
123 ******************************************************************************/
126 acpi_ev_fixed_event_initialize(void)
130 /* Initialize the structure that keeps track of fixed event handlers */
132 for (i
= 0; i
< NUM_FIXED_EVENTS
; i
++) {
133 acpi_gbl_fixed_event_handlers
[i
].handler
= NULL
;
134 acpi_gbl_fixed_event_handlers
[i
].context
= NULL
;
137 acpi_hw_register_bit_access (ACPI_WRITE
, ACPI_MTX_LOCK
, TMR_EN
, 0);
138 acpi_hw_register_bit_access (ACPI_WRITE
, ACPI_MTX_LOCK
, GBL_EN
, 0);
139 acpi_hw_register_bit_access (ACPI_WRITE
, ACPI_MTX_LOCK
, PWRBTN_EN
, 0);
140 acpi_hw_register_bit_access (ACPI_WRITE
, ACPI_MTX_LOCK
, SLPBTN_EN
, 0);
141 acpi_hw_register_bit_access (ACPI_WRITE
, ACPI_MTX_LOCK
, RTC_EN
, 0);
147 /******************************************************************************
149 * FUNCTION: Acpi_ev_fixed_event_detect
153 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
155 * DESCRIPTION: Checks the PM status register for fixed events
157 ******************************************************************************/
160 acpi_ev_fixed_event_detect(void)
162 u32 int_status
= INTERRUPT_NOT_HANDLED
;
167 * Read the fixed feature status and enable registers, as all the cases
168 * depend on their values.
171 status_register
= acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK
, PM1_STS
);
172 enable_register
= acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK
, PM1_EN
);
175 /* power management timer roll over */
177 if ((status_register
& ACPI_STATUS_PMTIMER
) &&
178 (enable_register
& ACPI_ENABLE_PMTIMER
)) {
179 int_status
|= acpi_ev_fixed_event_dispatch (ACPI_EVENT_PMTIMER
);
182 /* global event (BIOS want's the global lock) */
184 if ((status_register
& ACPI_STATUS_GLOBAL
) &&
185 (enable_register
& ACPI_ENABLE_GLOBAL
)) {
186 int_status
|= acpi_ev_fixed_event_dispatch (ACPI_EVENT_GLOBAL
);
189 /* power button event */
191 if ((status_register
& ACPI_STATUS_POWER_BUTTON
) &&
192 (enable_register
& ACPI_ENABLE_POWER_BUTTON
)) {
193 int_status
|= acpi_ev_fixed_event_dispatch (ACPI_EVENT_POWER_BUTTON
);
196 /* sleep button event */
198 if ((status_register
& ACPI_STATUS_SLEEP_BUTTON
) &&
199 (enable_register
& ACPI_ENABLE_SLEEP_BUTTON
)) {
200 int_status
|= acpi_ev_fixed_event_dispatch (ACPI_EVENT_SLEEP_BUTTON
);
207 /******************************************************************************
209 * FUNCTION: Acpi_ev_fixed_event_dispatch
211 * PARAMETERS: Event - Event type
213 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
215 * DESCRIPTION: Clears the status bit for the requested event, calls the
216 * handler that previously registered for the event.
218 ******************************************************************************/
221 acpi_ev_fixed_event_dispatch (
226 /* Clear the status bit */
229 case ACPI_EVENT_PMTIMER
:
230 register_id
= TMR_STS
;
233 case ACPI_EVENT_GLOBAL
:
234 register_id
= GBL_STS
;
237 case ACPI_EVENT_POWER_BUTTON
:
238 register_id
= PWRBTN_STS
;
241 case ACPI_EVENT_SLEEP_BUTTON
:
242 register_id
= SLPBTN_STS
;
246 register_id
= RTC_STS
;
254 acpi_hw_register_bit_access (ACPI_WRITE
, ACPI_MTX_DO_NOT_LOCK
, register_id
, 1);
257 * Make sure we've got a handler. If not, report an error.
258 * The event is disabled to prevent further interrupts.
260 if (NULL
== acpi_gbl_fixed_event_handlers
[event
].handler
) {
261 register_id
= (PM1_EN
| REGISTER_BIT_ID(register_id
));
263 acpi_hw_register_bit_access (ACPI_WRITE
, ACPI_MTX_DO_NOT_LOCK
,
267 ("Ev_gpe_dispatch: No installed handler for fixed event [%08X]\n",
270 return (INTERRUPT_NOT_HANDLED
);
273 /* Invoke the handler */
275 return ((acpi_gbl_fixed_event_handlers
[event
].handler
)(
276 acpi_gbl_fixed_event_handlers
[event
].context
));
280 /******************************************************************************
282 * FUNCTION: Acpi_ev_gpe_initialize
288 * DESCRIPTION: Initialize the GPE data structures
290 ******************************************************************************/
293 acpi_ev_gpe_initialize (void)
299 u16 gpe0register_count
;
300 u16 gpe1_register_count
;
304 * Set up various GPE counts
306 * You may ask,why are the GPE register block lengths divided by 2?
307 * From the ACPI 2.0 Spec, section, 4.7.1.6 General-Purpose Event
308 * Registers, we have,
310 * "Each register block contains two registers of equal length
311 * GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
312 * GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
313 * The length of the GPE1_STS and GPE1_EN registers is equal to
314 * half the GPE1_LEN. If a generic register block is not supported
315 * then its respective block pointer and block length values in the
316 * FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
317 * to be the same size."
320 gpe0register_count
= (u16
) DIV_2 (acpi_gbl_FADT
->gpe0blk_len
);
321 gpe1_register_count
= (u16
) DIV_2 (acpi_gbl_FADT
->gpe1_blk_len
);
322 acpi_gbl_gpe_register_count
= gpe0register_count
+ gpe1_register_count
;
324 if (!acpi_gbl_gpe_register_count
) {
325 REPORT_WARNING (("Zero GPEs are defined in the FADT\n"));
330 * Allocate the Gpe information block
333 acpi_gbl_gpe_registers
= acpi_cm_callocate (acpi_gbl_gpe_register_count
*
334 sizeof (ACPI_GPE_REGISTERS
));
335 if (!acpi_gbl_gpe_registers
) {
336 return (AE_NO_MEMORY
);
340 * Allocate the Gpe dispatch handler block
341 * There are eight distinct GP events per register.
342 * Initialization to zeros is sufficient
345 acpi_gbl_gpe_info
= acpi_cm_callocate (MUL_8 (acpi_gbl_gpe_register_count
) *
346 sizeof (ACPI_GPE_LEVEL_INFO
));
347 if (!acpi_gbl_gpe_info
) {
348 acpi_cm_free (acpi_gbl_gpe_registers
);
349 return (AE_NO_MEMORY
);
352 /* Set the Gpe validation table to GPE_INVALID */
354 MEMSET (acpi_gbl_gpe_valid
, (int) ACPI_GPE_INVALID
, NUM_GPE
);
357 * Initialize the Gpe information and validation blocks. A goal of these
358 * blocks is to hide the fact that there are two separate GPE register sets
359 * In a given block, the status registers occupy the first half, and
360 * the enable registers occupy the second half.
367 for (i
= 0; i
< gpe0register_count
; i
++) {
368 acpi_gbl_gpe_registers
[register_index
].status_addr
=
369 (u16
) (ACPI_GET_ADDRESS (acpi_gbl_FADT
->Xgpe0blk
.address
) + i
);
371 acpi_gbl_gpe_registers
[register_index
].enable_addr
=
372 (u16
) (ACPI_GET_ADDRESS (acpi_gbl_FADT
->Xgpe0blk
.address
) + i
+ gpe0register_count
);
374 acpi_gbl_gpe_registers
[register_index
].gpe_base
= (u8
) MUL_8 (i
);
376 for (j
= 0; j
< 8; j
++) {
377 gpe_number
= acpi_gbl_gpe_registers
[register_index
].gpe_base
+ j
;
378 acpi_gbl_gpe_valid
[gpe_number
] = (u8
) register_index
;
382 * Clear the status/enable registers. Note that status registers
383 * are cleared by writing a '1', while enable registers are cleared
386 acpi_os_out8 (acpi_gbl_gpe_registers
[register_index
].enable_addr
, 0x00);
387 acpi_os_out8 (acpi_gbl_gpe_registers
[register_index
].status_addr
, 0xFF);
394 for (i
= 0; i
< gpe1_register_count
; i
++) {
395 acpi_gbl_gpe_registers
[register_index
].status_addr
=
396 (u16
) (ACPI_GET_ADDRESS (acpi_gbl_FADT
->Xgpe1_blk
.address
) + i
);
398 acpi_gbl_gpe_registers
[register_index
].enable_addr
=
399 (u16
) (ACPI_GET_ADDRESS (acpi_gbl_FADT
->Xgpe1_blk
.address
) + i
+ gpe1_register_count
);
401 acpi_gbl_gpe_registers
[register_index
].gpe_base
=
402 (u8
) (acpi_gbl_FADT
->gpe1_base
+ MUL_8 (i
));
404 for (j
= 0; j
< 8; j
++) {
405 gpe_number
= acpi_gbl_gpe_registers
[register_index
].gpe_base
+ j
;
406 acpi_gbl_gpe_valid
[gpe_number
] = (u8
) register_index
;
410 * Clear the status/enable registers. Note that status registers
411 * are cleared by writing a '1', while enable registers are cleared
414 acpi_os_out8 (acpi_gbl_gpe_registers
[register_index
].enable_addr
, 0x00);
415 acpi_os_out8 (acpi_gbl_gpe_registers
[register_index
].status_addr
, 0xFF);
424 /******************************************************************************
426 * FUNCTION: Acpi_ev_save_method_info
432 * DESCRIPTION: Called from Acpi_walk_namespace. Expects each object to be a
433 * control method under the _GPE portion of the namespace.
434 * Extract the name and GPE type from the object, saving this
435 * information for quick lookup during GPE dispatch
437 * The name of each GPE control method is of the form:
440 * L - means that the GPE is level triggered
441 * E - means that the GPE is edge triggered
442 * nn - is the GPE number
444 ******************************************************************************/
447 acpi_ev_save_method_info (
448 ACPI_HANDLE obj_handle
,
454 NATIVE_CHAR name
[ACPI_NAME_SIZE
+ 1];
458 /* Extract the name from the object and convert to a string */
460 MOVE_UNALIGNED32_TO_32 (name
, &((ACPI_NAMESPACE_NODE
*) obj_handle
)->name
);
461 name
[ACPI_NAME_SIZE
] = 0;
464 * Edge/Level determination is based on the 2nd s8 of the method name
466 if (name
[1] == 'L') {
467 type
= ACPI_EVENT_LEVEL_TRIGGERED
;
469 else if (name
[1] == 'E') {
470 type
= ACPI_EVENT_EDGE_TRIGGERED
;
473 /* Unknown method type, just ignore it! */
478 /* Convert the last two characters of the name to the Gpe Number */
480 gpe_number
= STRTOUL (&name
[2], NULL
, 16);
481 if (gpe_number
== ACPI_UINT32_MAX
) {
482 /* Conversion failed; invalid method, just ignore it */
487 /* Ensure that we have a valid GPE number */
489 if (acpi_gbl_gpe_valid
[gpe_number
] == ACPI_GPE_INVALID
) {
490 /* Not valid, all we can do here is ignore it */
496 * Now we can add this information to the Gpe_info block
497 * for use during dispatch of this GPE.
500 acpi_gbl_gpe_info
[gpe_number
].type
= type
;
501 acpi_gbl_gpe_info
[gpe_number
].method_handle
= obj_handle
;
505 * Enable the GPE (SCIs should be disabled at this point)
508 acpi_hw_enable_gpe (gpe_number
);
514 /******************************************************************************
516 * FUNCTION: Acpi_ev_init_gpe_control_methods
522 * DESCRIPTION: Obtain the control methods associated with the GPEs.
524 * NOTE: Must be called AFTER namespace initialization!
526 ******************************************************************************/
529 acpi_ev_init_gpe_control_methods (void)
534 /* Get a permanent handle to the _GPE object */
536 status
= acpi_get_handle (NULL
, "\\_GPE", &acpi_gbl_gpe_obj_handle
);
537 if (ACPI_FAILURE (status
)) {
541 /* Traverse the namespace under \_GPE to find all methods there */
543 status
= acpi_walk_namespace (ACPI_TYPE_METHOD
, acpi_gbl_gpe_obj_handle
,
544 ACPI_UINT32_MAX
, acpi_ev_save_method_info
,
551 /******************************************************************************
553 * FUNCTION: Acpi_ev_gpe_detect
557 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
559 * DESCRIPTION: Detect if any GP events have occurred
561 ******************************************************************************/
564 acpi_ev_gpe_detect (void)
566 u32 int_status
= INTERRUPT_NOT_HANDLED
;
569 u8 enabled_status_byte
;
574 * Read all of the 8-bit GPE status and enable registers
575 * in both of the register blocks, saving all of it.
576 * Find all currently active GP events.
579 for (i
= 0; i
< acpi_gbl_gpe_register_count
; i
++) {
580 acpi_gbl_gpe_registers
[i
].status
=
581 acpi_os_in8 (acpi_gbl_gpe_registers
[i
].status_addr
);
583 acpi_gbl_gpe_registers
[i
].enable
=
584 acpi_os_in8 (acpi_gbl_gpe_registers
[i
].enable_addr
);
586 /* First check if there is anything active at all in this register */
588 enabled_status_byte
= (u8
) (acpi_gbl_gpe_registers
[i
].status
&
589 acpi_gbl_gpe_registers
[i
].enable
);
591 if (!enabled_status_byte
) {
592 /* No active GPEs in this register, move on */
597 /* Now look at the individual GPEs in this byte register */
599 for (j
= 0, bit_mask
= 1; j
< 8; j
++, bit_mask
<<= 1) {
600 /* Examine one GPE bit */
602 if (enabled_status_byte
& bit_mask
) {
604 * Found an active GPE. Dispatch the event to a handler
608 acpi_ev_gpe_dispatch (acpi_gbl_gpe_registers
[i
].gpe_base
+ j
);
617 /******************************************************************************
619 * FUNCTION: Acpi_ev_asynch_execute_gpe_method
621 * PARAMETERS: Gpe_number - The 0-based Gpe number
625 * DESCRIPTION: Perform the actual execution of a GPE control method. This
626 * function is called from an invocation of Acpi_os_queue_for_execution
627 * (and therefore does NOT execute at interrupt level) so that
628 * the control method itself is not executed in the context of
629 * the SCI interrupt handler.
631 ******************************************************************************/
634 acpi_ev_asynch_execute_gpe_method (
637 u32 gpe_number
= (u32
) context
;
638 ACPI_GPE_LEVEL_INFO gpe_info
;
642 * Take a snapshot of the GPE info for this level
644 acpi_cm_acquire_mutex (ACPI_MTX_EVENTS
);
645 gpe_info
= acpi_gbl_gpe_info
[gpe_number
];
646 acpi_cm_release_mutex (ACPI_MTX_EVENTS
);
649 * Method Handler (_Lxx, _Exx):
650 * ----------------------------
651 * Evaluate the _Lxx/_Exx control method that corresponds to this GPE.
653 if (gpe_info
.method_handle
) {
654 acpi_ns_evaluate_by_handle (gpe_info
.method_handle
, NULL
, NULL
);
660 * If level-triggered we clear the GPE status bit after handling the event.
662 if (gpe_info
.type
& ACPI_EVENT_LEVEL_TRIGGERED
) {
663 acpi_hw_clear_gpe (gpe_number
);
669 acpi_hw_enable_gpe (gpe_number
);
675 /******************************************************************************
677 * FUNCTION: Acpi_ev_gpe_dispatch
679 * PARAMETERS: Gpe_number - The 0-based Gpe number
681 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
683 * DESCRIPTION: Handle and dispatch a General Purpose Acpi_event.
684 * Clears the status bit for the requested event.
686 * TBD: [Investigate] is this still valid or necessary:
687 * The Gpe handler differs from the fixed events in that it clears the enable
688 * bit rather than the status bit to clear the interrupt. This allows
689 * software outside of interrupt context to determine what caused the SCI and
690 * dispatch the correct AML.
692 ******************************************************************************/
695 acpi_ev_gpe_dispatch (
698 ACPI_GPE_LEVEL_INFO gpe_info
;
703 if (acpi_gbl_gpe_valid
[gpe_number
] == ACPI_GPE_INVALID
) {
704 return (INTERRUPT_NOT_HANDLED
);
710 acpi_hw_disable_gpe (gpe_number
);
712 gpe_info
= acpi_gbl_gpe_info
[gpe_number
];
717 * If edge-triggered, clear the GPE status bit now. Note that
718 * level-triggered events are cleared after the GPE is serviced.
720 if (gpe_info
.type
& ACPI_EVENT_EDGE_TRIGGERED
) {
721 acpi_hw_clear_gpe (gpe_number
);
724 * Function Handler (e.g. EC)?
726 if (gpe_info
.handler
) {
727 /* Invoke function handler (at interrupt level). */
728 gpe_info
.handler (gpe_info
.context
);
730 /* Level-Triggered? */
731 if (gpe_info
.type
& ACPI_EVENT_LEVEL_TRIGGERED
) {
732 acpi_hw_clear_gpe (gpe_number
);
736 acpi_hw_enable_gpe (gpe_number
);
739 * Method Handler (e.g. _Exx/_Lxx)?
741 else if (gpe_info
.method_handle
) {
742 if (ACPI_FAILURE(acpi_os_queue_for_execution (OSD_PRIORITY_GPE
,
743 acpi_ev_asynch_execute_gpe_method
, (void*)(NATIVE_UINT
)gpe_number
))) {
745 * Shoudn't occur, but if it does report an error. Note that
746 * the GPE will remain disabled until the ACPI Core Subsystem
747 * is restarted, or the handler is removed/reinstalled.
749 REPORT_ERROR (("Acpi_ev_gpe_dispatch: Unable to queue handler for GPE bit [%X]\n", gpe_number
));
753 * No Handler? Report an error and leave the GPE disabled.
756 REPORT_ERROR (("Acpi_ev_gpe_dispatch: No installed handler for GPE [%X]\n", gpe_number
));
758 /* Level-Triggered? */
759 if (gpe_info
.type
& ACPI_EVENT_LEVEL_TRIGGERED
) {
760 acpi_hw_clear_gpe (gpe_number
);
764 return (INTERRUPT_HANDLED
);